Skip to content

Commit

Permalink
staging name diff
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreSlavescu committed Nov 9, 2024
1 parent bd7a34e commit df86b05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ Instead of testing on GPU MODE directly we can leverage a staging environment ca
### How to add the bot to a personal server

For testing purposes, bot can be run on a personal server as well. Follow the steps [here](https://discordjs.guide/preparations/setting-up-a-bot-application.html#creating-your-bot) and [here](https://discordjs.guide/preparations/adding-your-bot-to-servers.html#bot-invite-links) to create a bot application and then add it to your server.
After doing that, you can add a new environment variable called `DISCORD_DEBUG_TOKEN` to your `.env` file and set it to the bot token you got from the Discord Developer Portal. Then you can simply run the bot for your server by running `DEBUG=1 python discord-bot.py`. When the bot is run without `DEBUG=1`, it will run on the "Discord Cluster Staging" server.
After doing that, you can add a new environment variable called `DISCORD_DEBUG_TOKEN` to your `.env` file and set it to the bot token you got from the Discord Developer Portal.

You can run the bot in two modes:
- Production mode: `python discord-bot.py`
- Debug/staging mode: `python discord-bot.py --debug`

When running in debug mode, the bot will use your `DISCORD_DEBUG_TOKEN` and display as "Cluster Bot (Staging)" to clearly indicate it's not the production instance.

Bot needs to be invited using an oauth2 token and needs the `Message Content Intent` permission.

Expand Down
28 changes: 23 additions & 5 deletions discord-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import zipfile
import subprocess
import argparse

# Set up logging
logging.basicConfig(
Expand Down Expand Up @@ -49,9 +50,6 @@ def get_github_branch_name():
if not os.getenv('GITHUB_REPO'):
logger.error("GITHUB_REPO not found in environment variables")
raise ValueError("GITHUB_REPO not found")
if os.getenv("DEBUG") and not os.getenv('DISCORD_DEBUG_TOKEN'):
logger.error("DISCORD_DEBUG_TOKEN not found in environment variables for debug mode")
raise ValueError("DISCORD_DEBUG_TOKEN not found")

logger.info(f"Using GitHub repo: {os.getenv('GITHUB_REPO')}")

Expand Down Expand Up @@ -164,6 +162,15 @@ async def check_workflow_status(run_id, thread):
@client.event
async def on_ready():
logger.info(f'Logged in as {client.user}')
for guild in client.guilds:
try:
if debug_mode := os.getenv("DEBUG"):
await guild.me.edit(nick="Cluster Bot (Staging)")
else:
await guild.me.edit(nick="Cluster Bot")
logger.info(f'Updated nickname in guild: {guild.name}')
except Exception as e:
logger.warning(f'Failed to update nickname in guild {guild.name}: {e}')

@client.event
async def on_message(message):
Expand Down Expand Up @@ -232,7 +239,18 @@ async def on_message(message):

# Run the bot
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Run the Discord Cluster Bot')
parser.add_argument('--debug', action='store_true', help='Run in debug/staging mode')
args = parser.parse_args()

logger.info("Starting bot...")
if debug_mode := os.getenv("DEBUG"):
if args.debug:
logger.info("Running in debug mode")
client.run(os.getenv('DISCORD_DEBUG_TOKEN') if debug_mode else os.getenv('DISCORD_TOKEN'))
token = os.getenv('DISCORD_DEBUG_TOKEN')
if not token:
logger.error("DISCORD_DEBUG_TOKEN not found in environment variables")
raise ValueError("DISCORD_DEBUG_TOKEN not found")
else:
token = os.getenv('DISCORD_TOKEN')

client.run(token)

0 comments on commit df86b05

Please sign in to comment.