Setup channels

This commit is contained in:
Yohann D'ANELLO 2021-10-26 23:08:55 +02:00
parent 90f0c6e840
commit 27e7b70295
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 178 additions and 10 deletions

View File

@ -1,6 +1,6 @@
from pathlib import Path
import disnake import disnake
from disnake import CategoryChannel, PermissionOverwrite, TextChannel
from disnake.errors import InvalidData
from disnake.ext import commands from disnake.ext import commands
import logging import logging
@ -10,8 +10,146 @@ bot = commands.Bot(command_prefix='!')
@bot.event @bot.event
async def on_message(message: disnake.Message): async def on_ready():
await bot.process_commands(message) config: Config = bot.config
logger = bot.logger
if config.guild is None:
config.save()
logger.error("The guild ID is missing")
exit(1)
guild = await bot.fetch_guild(config.guild)
if not guild:
logger.error("Unknown guild.")
exit(1)
if config.vote_category is None:
category = await guild.create_category("Votes")
config.vote_category = category.id
config.save()
if config.secret_category is None:
category = await guild.create_category("Conversation⋅s secrète⋅s")
config.secret_category = category.id
config.save()
vote_category: CategoryChannel = await guild.fetch_channel(config.vote_category)
if vote_category is None:
config.vote_category = None
return await on_ready()
secret_category: CategoryChannel = await guild.fetch_channel(config.secret_category)
if secret_category is None:
config.secret_category = None
return await on_ready()
await vote_category.set_permissions(
guild.default_role, overwrite=PermissionOverwrite(read_message_history=False, read_messages=False)
)
await secret_category.set_permissions(
guild.default_role, overwrite=PermissionOverwrite(read_message_history=False, read_messages=False)
)
for i, player in enumerate(Config.PLAYERS):
player_id = player.lower()
if player_id not in config.vote_channels:
channel: TextChannel = await vote_category.create_text_channel(player_id)
config.vote_channels[player_id] = channel.id
config.save()
channel: TextChannel = await guild.fetch_channel(config.vote_channels[player_id])
if channel is None:
del config.vote_channels[player_id]
return await on_ready()
await channel.edit(name=player_id, category=vote_category, position=i)
await channel.set_permissions(
guild.default_role, overwrite=PermissionOverwrite(read_message_history=False, read_messages=False)
)
if player_id not in config.player_roles:
role = await guild.create_role(name=player)
config.player_roles[player_id] = role.id
config.save()
guild = await bot.fetch_guild(guild.id) # update roles
role = guild.get_role(config.player_roles[player_id])
if role is None:
del config.player_roles[player_id]
config.save()
return await on_ready()
await channel.set_permissions(
role, overwrite=PermissionOverwrite(read_message_history=True, read_messages=True)
)
if not config.telepathy_channel:
channel: TextChannel = await secret_category.create_text_channel("bigbrain")
config.telepathy_channel = channel.id
config.save()
telepathy_channel: TextChannel = await guild.fetch_channel(config.telepathy_channel)
if not telepathy_channel:
config.telepathy_channel = None
return await on_ready()
await telepathy_channel.edit(name="bigbrain", category=secret_category, position=0,
topic="Échanges télépathiques")
await telepathy_channel.set_permissions(
guild.default_role, overwrite=PermissionOverwrite(read_message_history=False, read_messages=False)
)
delphine = guild.get_role(config.player_roles['delphine'])
philia = guild.get_role(config.player_roles['philia'])
await telepathy_channel.set_permissions(
delphine, overwrite=PermissionOverwrite(read_message_history=True, read_messages=True)
)
await telepathy_channel.set_permissions(
philia, overwrite=PermissionOverwrite(read_message_history=True, read_messages=True)
)
if not config.left_channel:
channel: TextChannel = await secret_category.create_text_channel("left")
config.left_channel = channel.id
config.save()
left_channel: TextChannel = await guild.fetch_channel(config.left_channel)
if not left_channel:
config.left_channel = None
return await on_ready()
await left_channel.edit(name="left", category=secret_category, position=1,
topic="Des voix dans la tête ...")
await left_channel.set_permissions(
guild.default_role, overwrite=PermissionOverwrite(read_message_history=False, read_messages=False)
)
await left_channel.set_permissions(
philia, overwrite=PermissionOverwrite(read_message_history=True, read_messages=True)
)
if not config.backdoor_channel:
channel: TextChannel = await secret_category.create_text_channel("backdoor")
config.backdoor_channel = channel.id
config.save()
backdoor_channel: TextChannel = await guild.fetch_channel(config.backdoor_channel)
if not backdoor_channel:
config.backdoor_channel = None
return await on_ready()
await backdoor_channel.edit(name="backdoor", category=secret_category, position=2,
topic="Panel d'administrati0n du jeu")
await backdoor_channel.set_permissions(
guild.default_role, overwrite=PermissionOverwrite(read_message_history=False, read_messages=False)
)
dan = guild.get_role(config.player_roles['dan'])
await backdoor_channel.set_permissions(
dan, overwrite=PermissionOverwrite(read_message_history=True, read_messages=True)
)
config.save()
@bot.command() @bot.command()
@ -37,8 +175,7 @@ class Confirm(disnake.ui.View):
def run(): def run():
config_file = Path(__file__).parent.parent / 'config.yml' config = Config.load()
config = Config.load(config_file)
logger = logging.getLogger('discord') logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)

View File

@ -1,13 +1,44 @@
from dataclasses import dataclass from dataclasses import asdict, dataclass, field
from pathlib import Path
import yaml import yaml
@dataclass @dataclass
class Config: class Config:
discord_token: str PLAYERS = [
"Kamui",
"Oji",
"Dan",
"Tora",
"Delphine",
"Philia",
"Hanabi",
"Nona",
"Ennea",
]
discord_token: str = ""
guild: int = None
vote_category: int = None
secret_category: int = None
vote_channels: dict[str, int] = field(default_factory=dict)
backdoor_channel: int = None
telepathy_channel: int = None
left_channel: int = None
player_roles: dict[str, int] = field(default_factory=dict)
@classmethod @classmethod
def load(cls, filename) -> "Config": def load(cls, filename=None) -> "Config":
if filename is None:
filename = Path(__file__).parent.parent / 'config.yml'
with open(filename) as config_file: with open(filename) as config_file:
return Config(**yaml.safe_load(config_file)) return Config(**(yaml.safe_load(config_file) or {}))
def save(self, filename=None) -> None:
if filename is None:
filename = Path(__file__).parent.parent / 'config.yml'
with open(filename, 'w') as config_file:
yaml.safe_dump(asdict(self), config_file)