Small code refactoring
This commit is contained in:
parent
8c78d1c968
commit
90f0c6e840
54
bot.py
54
bot.py
|
@ -1,55 +1,7 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from dataclasses import dataclass
|
from orochi import bot
|
||||||
import disnake
|
|
||||||
from disnake.ext import commands
|
|
||||||
import logging
|
|
||||||
import yaml
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
if __name__ == '__main__':
|
||||||
class Config:
|
bot.run()
|
||||||
discord_token: str
|
|
||||||
|
|
||||||
|
|
||||||
with open('config.yml') as config_file:
|
|
||||||
config = Config(**yaml.safe_load(config_file))
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('discord')
|
|
||||||
logger.setLevel(logging.DEBUG)
|
|
||||||
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
|
|
||||||
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
|
||||||
logger.addHandler(handler)
|
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix='!')
|
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
|
||||||
async def on_message(message: disnake.Message):
|
|
||||||
await bot.process_commands(message)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.command()
|
|
||||||
async def vote(ctx: commands.Context):
|
|
||||||
view = Confirm()
|
|
||||||
await ctx.message.reply("plop", view=view)
|
|
||||||
await view.wait()
|
|
||||||
|
|
||||||
|
|
||||||
# Define a simple View that gives us a confirmation menu
|
|
||||||
class Confirm(disnake.ui.View):
|
|
||||||
@disnake.ui.button(label="S'allier", style=disnake.ButtonStyle.green)
|
|
||||||
async def confirm(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
|
|
||||||
self.clear_items()
|
|
||||||
await interaction.response.edit_message(content="Vous vous êtes allié.", view=self)
|
|
||||||
self.stop()
|
|
||||||
|
|
||||||
@disnake.ui.button(label="Trahir", style=disnake.ButtonStyle.red)
|
|
||||||
async def cancel(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
|
|
||||||
self.clear_items()
|
|
||||||
await interaction.response.edit_message(content="Vous avez trahi.", view=self)
|
|
||||||
self.stop()
|
|
||||||
|
|
||||||
|
|
||||||
bot.run(config.discord_token)
|
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import disnake
|
||||||
|
from disnake.ext import commands
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from orochi.config import Config
|
||||||
|
|
||||||
|
bot = commands.Bot(command_prefix='!')
|
||||||
|
|
||||||
|
|
||||||
|
@bot.event
|
||||||
|
async def on_message(message: disnake.Message):
|
||||||
|
await bot.process_commands(message)
|
||||||
|
|
||||||
|
|
||||||
|
@bot.command()
|
||||||
|
async def vote(ctx: commands.Context):
|
||||||
|
view = Confirm()
|
||||||
|
await ctx.message.reply("plop", view=view)
|
||||||
|
await view.wait()
|
||||||
|
|
||||||
|
|
||||||
|
# Define a simple View that gives us a confirmation menu
|
||||||
|
class Confirm(disnake.ui.View):
|
||||||
|
@disnake.ui.button(label="S'allier", style=disnake.ButtonStyle.green)
|
||||||
|
async def confirm(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
|
||||||
|
self.clear_items()
|
||||||
|
await interaction.response.edit_message(content="Vous vous êtes allié.", view=self)
|
||||||
|
self.stop()
|
||||||
|
|
||||||
|
@disnake.ui.button(label="Trahir", style=disnake.ButtonStyle.red)
|
||||||
|
async def cancel(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
|
||||||
|
self.clear_items()
|
||||||
|
await interaction.response.edit_message(content="Vous avez trahi.", view=self)
|
||||||
|
self.stop()
|
||||||
|
|
||||||
|
|
||||||
|
def run():
|
||||||
|
config_file = Path(__file__).parent.parent / 'config.yml'
|
||||||
|
config = Config.load(config_file)
|
||||||
|
|
||||||
|
logger = logging.getLogger('discord')
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
handler = logging.FileHandler(filename='../discord.log', encoding='utf-8', mode='w')
|
||||||
|
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
|
||||||
|
logger.addHandler(handler)
|
||||||
|
|
||||||
|
bot.config = config
|
||||||
|
bot.logger = logger
|
||||||
|
|
||||||
|
bot.run(config.discord_token)
|
|
@ -0,0 +1,13 @@
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Config:
|
||||||
|
discord_token: str
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load(cls, filename) -> "Config":
|
||||||
|
with open(filename) as config_file:
|
||||||
|
return Config(**yaml.safe_load(config_file))
|
Loading…
Reference in New Issue