Files
.gitignore
bot.py
README.md
requirements.txt
Code
4 300
bot.py
import logging
import os
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
logger = logging.getLogger("friends-server-bot")
class FriendsServerBot(commands.Bot):
def __init__(self) -> None:
intents = discord.Intents.default()
intents.guilds = True
super().__init__(command_prefix="!", intents=intents)
async def setup_hook(self) -> None:
await self.tree.sync()
logger.info("Application commands synced")
async def on_ready(self) -> None:
if self.user is not None:
logger.info("Logged in as %s (%s)", self.user, self.user.id)
bot = FriendsServerBot()
def find_role(guild: discord.Guild, name: str) -> discord.Role | None:
return discord.utils.get(guild.roles, name=name)
def find_category(guild: discord.Guild, name: str) -> discord.CategoryChannel | None:
return discord.utils.get(guild.categories, name=name)
async def get_or_create_role(
guild: discord.Guild,
name: str,
colour: discord.Colour,
) -> tuple[discord.Role, bool]:
role = find_role(guild, name)
if role is not None:
return role, False
return await guild.create_role(
name=name,
colour=colour,
reason="Friends server setup",
), True
async def get_or_create_category(
guild: discord.Guild,
name: str,
) -> tuple[discord.CategoryChannel, bool]:
category = find_category(guild, name)
if category is not None:
return category, False
return await guild.create_category(name, reason="Friends server setup"), True
async def get_or_create_text_channel(
category: discord.CategoryChannel,
name: str,
topic: str | None = None,
) -> tuple[discord.TextChannel, bool]:
channel = discord.utils.get(category.text_channels, name=name)
if channel is not None:
return channel, False
return await category.create_text_channel(
name,
topic=topic,
reason="Friends server setup",
), True
async def get_or_create_voice_channel(
category: discord.CategoryChannel,
name: str,
) -> tuple[discord.VoiceChannel, bool]:
channel = discord.utils.get(category.voice_channels, name=name)
if channel is not None:
return channel, False
return await category.create_voice_channel(
name,
reason="Friends server setup",
), True
@bot.tree.command(name="setup-server", description="Create the friend-server channels and roles")
@app_commands.checks.has_permissions(administrator=True)
@app_commands.checks.cooldown(1, 30.0)
async def setup_server(interaction: discord.Interaction) -> None:
guild = interaction.guild
if guild is None:
await interaction.response.send_message(
"This command can only be used inside a server.",
ephemeral=True,
)
return
await interaction.response.defer(ephemeral=True)
try:
role_definitions = (
("Admin", discord.Colour.red()),
("Moderator", discord.Colour.blue()),
("Gamer", discord.Colour.green()),
("Music Lover", discord.Colour.purple()),
("Member", discord.Colour.light_grey()),
)
role_results = [
await get_or_create_role(guild, name, colour)
for name, colour in role_definitions
]
welcome_category, welcome_category_created = await get_or_create_category(
guild, "Welcome"
)
welcome_channel, welcome_channel_created = await get_or_create_text_channel(
welcome_category,
"welcome",
"Welcome to the server! Read the rules and say hello.",
)
rules_channel, rules_channel_created = await get_or_create_text_channel(
welcome_category,
"rules",
"Server rules and guidelines.",
)
music_category, music_category_created = await get_or_create_category(
guild, "Loft Music"
)
music_channel, music_channel_created = await get_or_create_text_channel(
music_category,
"loft-music",
"Share songs, playlists, and music recommendations.",
)
music_voice, music_voice_created = await get_or_create_voice_channel(
music_category, "Music Lounge"
)
gaming_category, gaming_category_created = await get_or_create_category(
guild, "Gaming Chats"
)
gaming_channel, gaming_channel_created = await get_or_create_text_channel(
gaming_category,
"gaming-chat",
"Talk about games and find people to play with.",
)
gaming_voice, gaming_voice_created = await get_or_create_voice_channel(
gaming_category, "Gaming Lobby"
)
created_roles = sum(created for _, created in role_results)
created_channels = sum(
(
welcome_category_created,
welcome_channel_created,
rules_channel_created,
music_category_created,
music_channel_created,
music_voice_created,
gaming_category_created,
gaming_channel_created,
gaming_voice_created,
)
)
await welcome_channel.send(
"Welcome to the server! Check <#{}> and then introduce yourself.".format(
rules_channel.id
)
)
await rules_channel.send(
"Be respectful, keep chat friendly, and have fun."
)
await interaction.followup.send(
"Server setup complete. Created {} role(s) and {} channel/category item(s).\n"
"Added: **Loft Music** with `#loft-music` and **Music Lounge`; "
"**Gaming Chats** with `#gaming-chat` and **Gaming Lobby`.".format(
created_roles, created_channels
),
ephemeral=True,
)
logger.info("Server setup completed for guild %s (%s)", guild.name, guild.id)
except discord.Forbidden: