Skip to main content

Overview

Sakuya AC supports real-time chat synchronization between Discord and your YSFlight server. Players can communicate across platforms, with messages automatically relayed in both directions.
Discord messages appear in-game with the [Discord] prefix, and in-game messages are sent to your configured Discord channel.

Features

  • Bidirectional messaging - Discord ↔ YSFlight chat sync
  • Automatic @mention sanitization - Prevents @everyone and @here abuse
  • Bot message filtering - Ignores messages from other bots
  • Player join/leave notifications - Discord alerts when players connect/disconnect
  • Non-blocking operation - Runs asynchronously without impacting server performance

Prerequisites

Python Dependencies

Discord integration requires additional Python packages:
pip install -r requirements.txt
On modern Linux distributions, you may need to create a virtual environment:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Required Dependencies

The integration uses:
  • aiohttp - Asynchronous HTTP client for Discord API
  • asyncio - Async runtime (included in Python 3.7+)

Setting Up Discord Bot

1

Create Discord Application

  1. Go to Discord Developer Portal
  2. Click New Application
  3. Enter a name (e.g., “YSFlight Chat Bot”)
  4. Click Create
2

Create Bot User

  1. Navigate to the Bot section in the left sidebar
  2. Click Add Bot
  3. Confirm by clicking Yes, do it!
  4. Customize the bot’s username and avatar if desired
3

Configure Bot Permissions

Enable the Message Content Intent:
  1. Scroll to Privileged Gateway Intents
  2. Enable Message Content Intent
  3. Click Save Changes
Without Message Content Intent, the bot cannot read messages from users.
4

Get Bot Token

  1. In the Bot section, find the Token area
  2. Click Reset Token (or Copy if visible)
  3. Copy the token and save it securely
Never share your bot token! Treat it like a password. If exposed, reset it immediately in the Developer Portal.
5

Invite Bot to Server

  1. Go to OAuth2 > URL Generator
  2. Select scopes:
    • bot
  3. Select bot permissions:
    • Read Messages/View Channels
    • Send Messages
    • Read Message History
  4. Copy the generated URL and open it in your browser
  5. Select your server and authorize
6

Get Channel ID

  1. Enable Developer Mode in Discord:
    • User Settings > Advanced > Developer Mode
  2. Right-click the channel you want to use
  3. Click Copy Channel ID
  4. Save this ID for configuration

Configuration

Edit your config.py file with the Discord settings:
config.py
# Discord chat integration
DISCORD_ENABLED = True

# Your bot token from the Developer Portal
DISCORD_TOKEN = "MTIzNDU2Nzg5MDEyMzQ1Njc4OQ.GhIjKl.MnOpQrStUvWxYzAbCdEfGhIjKlMnOpQrStUv"

# Channel ID where messages will be synced
CHANNEL_ID = 1234567890123456789
DISCORD_ENABLED
bool
required
Set to True to enable Discord integration, False to disable.
DISCORD_TOKEN
str
required
Your Discord bot token from the Developer Portal.
CHANNEL_ID
int
required
The Discord channel ID where messages will be synchronized.

How It Works

Message Flow

Discord → YSFlight:
[Discord User]: Hello from Discord!
Appears in-game as:
[Discord] Discord User: Hello from Discord!
YSFlight → Discord:
[Sakuya]: Hello from YSFlight!
Sent to Discord as:
Sakuya: Hello from YSFlight!

Player Notifications

When players join or leave, Discord receives notifications:
Sakuya has joined the server!
Sakuya has left the server!

Security Features

The integration automatically removes @everyone and @here mentions from Discord messages before relaying them to the game:
# In discordSync.py
message = re.sub(r'@(?:everyone|here)', '', message)
This prevents Discord users from spamming mass mentions through the game chat.
Messages from bots (including the integration bot itself) are automatically ignored to prevent message loops:
if author.get('bot'):
    return  # Skip bot messages

Implementation Details

Discord API Communication

The integration uses Discord’s REST API (v10) with the following endpoints: Sending Messages:
POST https://discord.com/api/v10/channels/{channel_id}/messages
Fetching Messages:
GET https://discord.com/api/v10/channels/{channel_id}/messages?limit=1&after={last_message_id}

Polling Mechanism

The system polls Discord every 1 second for new messages:
async def monitor_channel(channel_id, playerList:list):
    last_message_id = None
    while True:
        messages = await discord_fetch_messages(channel_id, last_message_id)
        # Process new messages
        await asyncio.sleep(1)  # Poll every second
This polling interval balances responsiveness with API rate limits. You can adjust it in lib/discordSync.py:98 if needed.

Broadcasting to Players

When a Discord message is received, it’s broadcast to all connected players:
encoded_msg = txtMsgr.encode(f"[Discord] {username}: {content}", True)
for player in playerList:
    player.streamWriterObject.write(encoded_msg)
    await player.streamWriterObject.drain()
Disconnected players are automatically removed from the broadcast list.

Troubleshooting

Bot not receiving messages

Verify in the Developer Portal:
  1. Go to your application
  2. Navigate to Bot
  3. Scroll to Privileged Gateway Intents
  4. Ensure Message Content Intent is enabled
  5. Save changes and restart your server
Check your DISCORD_TOKEN in config.py:
  • Tokens should be 70+ characters long
  • They contain letters, numbers, and special characters
  • If unsure, reset the token in the Developer Portal
Verify your CHANNEL_ID:
  • Channel IDs are 17-19 digit numbers
  • Must be an integer, not a string
  • The bot must have access to this channel

Bot not sending messages

Ensure the bot has these permissions in the channel:
  • Read Messages/View Channels
  • Send Messages
  • Read Message History
Check channel-specific permissions overrides.
Check your server logs for Discord API errors:
LOGGING_LEVEL = DEBUG  # In config.py
Common errors:
  • 401 Unauthorized - Invalid token
  • 403 Forbidden - Missing permissions
  • 404 Not Found - Invalid channel ID

Dependencies not installing

Modern Linux distros require virtual environments:
# Create venv
python3 -m venv venv

# Activate venv
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate     # Windows

# Install dependencies
pip install -r requirements.txt

# Run server (keep venv activated)
python main.py
The integration requires Python 3.7+ for asyncio support:
python --version
If you have an older version, upgrade Python before installing dependencies.

Performance Considerations

  • Asynchronous operation: Discord integration runs in the background without blocking game packets
  • Connection pooling: Uses aiohttp session pooling for efficient API requests
  • Player cleanup: Disconnected players are automatically removed from broadcast lists
  • Rate limiting: 1-second polling interval respects Discord’s rate limits

Security Best Practices

Protect Your Token

Never commit DISCORD_TOKEN to version control. Use environment variables or a separate credentials file that’s gitignored.

Limit Bot Permissions

Only grant the minimum required permissions. The bot doesn’t need administrator rights.

Monitor API Usage

Discord has rate limits. If you experience issues, check the polling interval in discordSync.py.

Use Read-Only Roles

Consider creating a read-only role for the bot in sensitive channels to prevent accidental message sends.

Advanced Configuration

Custom Message Formatting

To customize how Discord messages appear in-game, edit lib/discordSync.py:84:
# Default format
encoded_msg = txtMsgr.encode(f"[Discord] {username}: {content}", True)

# Custom format
encoded_msg = txtMsgr.encode(f"💬 {username} from Discord: {content}", True)

Adjust Polling Rate

To change how often Discord is checked for new messages, modify lib/discordSync.py:98:
await asyncio.sleep(1)  # Default: 1 second
await asyncio.sleep(0.5)  # Faster: 0.5 seconds (higher API usage)
await asyncio.sleep(2)  # Slower: 2 seconds (lower API usage)
Faster polling increases API requests. Discord may rate limit your bot if you poll too frequently.

Build docs developers (and LLMs) love