From 218e03c66f3265d5ec778386bb589f928beab72d Mon Sep 17 00:00:00 2001 From: shun <1ntegrale9uation@gmail.com> Date: Tue, 3 Oct 2023 17:43:55 +0900 Subject: [PATCH] =?UTF-8?q?extensions=E5=91=A8=E3=82=8A=E3=81=AE=E4=BB=95?= =?UTF-8?q?=E6=A7=98=E3=82=92dpy=20v2=E3=81=AB=E8=BF=BD=E5=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/errors.py | 9 ------ constants/discord.py | 6 ++++ extensions/__init__.py | 0 extensions/handle_error.py | 36 ++++++++++++++++++++++ {cogs => extensions}/players.py | 4 +-- {cogs => extensions}/status.py | 8 ++--- {cogs => extensions}/vote.py | 8 ++--- main.py | 54 ++++++++++++++------------------- requirements.txt | 1 + utils/__init__.py | 0 utils/errors.py | 12 ++++++++ 11 files changed, 87 insertions(+), 51 deletions(-) delete mode 100644 application/errors.py create mode 100644 constants/discord.py create mode 100644 extensions/__init__.py create mode 100644 extensions/handle_error.py rename {cogs => extensions}/players.py (94%) rename {cogs => extensions}/status.py (93%) rename {cogs => extensions}/vote.py (97%) create mode 100644 utils/__init__.py create mode 100644 utils/errors.py diff --git a/application/errors.py b/application/errors.py deleted file mode 100644 index 656d79a..0000000 --- a/application/errors.py +++ /dev/null @@ -1,9 +0,0 @@ -class PermissionNotFound(Exception): - pass - - -class NotGuildChannel(Exception): - pass - -class NotDMChannel(Exception): - pass diff --git a/constants/discord.py b/constants/discord.py new file mode 100644 index 0000000..e6f1a41 --- /dev/null +++ b/constants/discord.py @@ -0,0 +1,6 @@ +from dotenv import load_dotenv +from os import getenv + +load_dotenv() + +TOKEN = getenv('DISCORD_BOT_TOKEN') diff --git a/extensions/__init__.py b/extensions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/extensions/handle_error.py b/extensions/handle_error.py new file mode 100644 index 0000000..13806ff --- /dev/null +++ b/extensions/handle_error.py @@ -0,0 +1,36 @@ +import traceback +from discord.ext import commands +from utils.errors import PermissionNotFound, NotGuildChannel, NotDMChannel + + +class HandleErrorCog(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.Cog.listener() + async def on_command_error(ctx, error: commands.CommandError): + """エラーハンドリング""" + + if isinstance(error, commands.CheckFailure): + return + + if isinstance(error, PermissionNotFound): + await ctx.send('コマンドを実行する権限がありません') + return + + if isinstance(error, NotGuildChannel): + await ctx.send('サーバー内でのみ実行できるコマンドです') + return + + if isinstance(error, NotDMChannel): + await ctx.send('DM内でのみ実行できるコマンドです') + return + + orig_error = getattr(error, "original", error) + error_msg = ''.join(traceback.TracebackException.from_exception(orig_error).format()) + error_msg = "```py\n" + error_msg + "\n```" + await ctx.send(error_msg) + + +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(HandleErrorCog(bot)) diff --git a/cogs/players.py b/extensions/players.py similarity index 94% rename from cogs/players.py rename to extensions/players.py index 324610a..16bea93 100644 --- a/cogs/players.py +++ b/extensions/players.py @@ -36,5 +36,5 @@ async def leave(self, ctx): return await ctx.send("ゲームに参加していません。") -def setup(bot): - bot.add_cog(PlayersCog(bot)) +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(PlayersCog(bot)) diff --git a/cogs/status.py b/extensions/status.py similarity index 93% rename from cogs/status.py rename to extensions/status.py index ff5d590..43a70b8 100644 --- a/cogs/status.py +++ b/extensions/status.py @@ -1,7 +1,7 @@ import random from discord.ext import commands -from application.roles import simple -from application.errors import PermissionNotFound, NotGuildChannel +from utils.errors import PermissionNotFound, NotGuildChannel +from constants.roles import simple class GameStatus(commands.Cog): @@ -79,5 +79,5 @@ async def game_status(self, ctx): await ctx.send(f'現在の game.status は {self.bot.game.status} です') -def setup(bot): - bot.add_cog(GameStatus(bot)) +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(GameStatus(bot)) diff --git a/cogs/vote.py b/extensions/vote.py similarity index 97% rename from cogs/vote.py rename to extensions/vote.py index d3d713d..9177557 100644 --- a/cogs/vote.py +++ b/extensions/vote.py @@ -1,12 +1,12 @@ import discord from discord.ext import commands -from application import errors +from utils import errors from application.game import Game from application.player import Players from application.pagenator import Pagenator -class Vote(commands.Cog): +class VoteCog(commands.Cog): def __init__(self, bot): self.bot = bot @@ -116,5 +116,5 @@ async def werewolfs(self, ctx): await ctx.send(f'この村の人狼は {werewolfs} です。') -def setup(bot): - return bot.add_cog(Vote(bot)) +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(VoteCog(bot)) diff --git a/main.py b/main.py index b25dc65..1396a32 100644 --- a/main.py +++ b/main.py @@ -1,44 +1,34 @@ -import os -import traceback +import discord from discord.ext import commands from application.game import Game -from application.errors import PermissionNotFound, NotGuildChannel, NotDMChannel - -bot = commands.Bot(command_prefix='/') -bot.game = Game() +from constants.discord import TOKEN extensions = [ - 'cogs.status', - 'cogs.players', - 'cogs.vote', + 'handle_error', + 'players', + 'status', + 'vote', ] -for extension in extensions: - bot.load_extension(extension) - - -@bot.event -async def on_command_error(ctx, error): - """エラーハンドリング""" - if isinstance(error, commands.CheckFailure): - return - if isinstance(error, PermissionNotFound): - await ctx.send('コマンドを実行する権限がありません') - return +class WerewolfBot(commands.Bot): + def __init__(self): + super().__init__( + command_prefix=commands.when_mentioned_or('$'), + intents=discord.Intents.all(), + ) - if isinstance(error, NotGuildChannel): - await ctx.send('サーバー内でのみ実行できるコマンドです') - return + async def setup_hook(self): + for extension in extensions: + await self.load_extension(f'extensions.{extension}') + # await self.tree.sync() - if isinstance(error, NotDMChannel): - await ctx.send('DM内でのみ実行できるコマンドです') - return - orig_error = getattr(error, "original", error) - error_msg = ''.join(traceback.TracebackException.from_exception(orig_error).format()) - error_msg = "```py\n" + error_msg + "\n```" - await ctx.send(error_msg) +def main(): + bot = WerewolfBot() + bot.game = Game() + bot.run(TOKEN) -bot.run(os.environ['DISCORD_BOT_WEREWOLF_TOKEN']) +if __name__ == '__main__': + main() diff --git a/requirements.txt b/requirements.txt index 787b1ca..0e9007f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ discord.py[voice]>=2.3.1 +dotenv diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/errors.py b/utils/errors.py new file mode 100644 index 0000000..7fe59b4 --- /dev/null +++ b/utils/errors.py @@ -0,0 +1,12 @@ +import discord + +class PermissionNotFound(discord.DiscordException): + pass + + +class NotGuildChannel(discord.DiscordException): + pass + + +class NotDMChannel(discord.DiscordException): + pass