From d0b155b1b3a106728adaded34e3c8cb75d82e24f Mon Sep 17 00:00:00 2001 From: shun <1ntegrale9uation@gmail.com> Date: Tue, 3 Oct 2023 14:51:11 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=E7=92=B0=E5=A2=83=E3=82=92=E6=9B=B4?= =?UTF-8?q?=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Procfile | 2 +- discordbot.py => main.py | 0 requirements.txt | 2 +- runtime.txt | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename discordbot.py => main.py (100%) diff --git a/Procfile b/Procfile index d0caaaa..629b83a 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -discordbot: python discordbot.py +web: python main.py diff --git a/discordbot.py b/main.py similarity index 100% rename from discordbot.py rename to main.py diff --git a/requirements.txt b/requirements.txt index b96610e..787b1ca 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -discord.py[voice]>=1.4.1 +discord.py[voice]>=2.3.1 diff --git a/runtime.txt b/runtime.txt index 43b47fb..76b6e49 100644 --- a/runtime.txt +++ b/runtime.txt @@ -1 +1 @@ -python-3.8.5 +python-3.11.6 From bbaabc569f3402915603ea929d51eae620f62173 Mon Sep 17 00:00:00 2001 From: shun <1ntegrale9uation@gmail.com> Date: Tue, 3 Oct 2023 15:00:42 +0900 Subject: [PATCH 2/5] =?UTF-8?q?cogs/utils=20=E3=83=87=E3=82=A3=E3=83=AC?= =?UTF-8?q?=E3=82=AF=E3=83=88=E3=83=AA=E3=82=92=E5=BB=83=E6=AD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/__init__.py | 0 {cogs/utils => application}/errors.py | 0 {cogs/utils => application}/game.py | 2 +- {cogs/utils => application}/pagenator.py | 0 {cogs/utils => application}/player.py | 0 {cogs/utils => application}/roles.py | 0 cogs/players.py | 2 +- cogs/status.py | 4 ++-- cogs/vote.py | 8 ++++---- main.py | 4 ++-- tests/test_game.py | 4 ++-- 11 files changed, 12 insertions(+), 12 deletions(-) create mode 100644 application/__init__.py rename {cogs/utils => application}/errors.py (100%) rename {cogs/utils => application}/game.py (97%) rename {cogs/utils => application}/pagenator.py (100%) rename {cogs/utils => application}/player.py (100%) rename {cogs/utils => application}/roles.py (100%) diff --git a/application/__init__.py b/application/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cogs/utils/errors.py b/application/errors.py similarity index 100% rename from cogs/utils/errors.py rename to application/errors.py diff --git a/cogs/utils/game.py b/application/game.py similarity index 97% rename from cogs/utils/game.py rename to application/game.py index adf39fd..e013e41 100644 --- a/cogs/utils/game.py +++ b/application/game.py @@ -1,5 +1,5 @@ from typing import Optional -from cogs.utils.player import Player, Players +from application.player import Player, Players class Game(): diff --git a/cogs/utils/pagenator.py b/application/pagenator.py similarity index 100% rename from cogs/utils/pagenator.py rename to application/pagenator.py diff --git a/cogs/utils/player.py b/application/player.py similarity index 100% rename from cogs/utils/player.py rename to application/player.py diff --git a/cogs/utils/roles.py b/application/roles.py similarity index 100% rename from cogs/utils/roles.py rename to application/roles.py diff --git a/cogs/players.py b/cogs/players.py index 9557bd6..324610a 100644 --- a/cogs/players.py +++ b/cogs/players.py @@ -1,5 +1,5 @@ from discord.ext import commands -from cogs.utils.player import Player +from application.player import Player class PlayersCog(commands.Cog): diff --git a/cogs/status.py b/cogs/status.py index e69d4e3..ff5d590 100644 --- a/cogs/status.py +++ b/cogs/status.py @@ -1,7 +1,7 @@ import random from discord.ext import commands -from cogs.utils.roles import simple -from cogs.utils.errors import PermissionNotFound, NotGuildChannel +from application.roles import simple +from application.errors import PermissionNotFound, NotGuildChannel class GameStatus(commands.Cog): diff --git a/cogs/vote.py b/cogs/vote.py index eba9f06..d3d713d 100644 --- a/cogs/vote.py +++ b/cogs/vote.py @@ -1,9 +1,9 @@ import discord from discord.ext import commands -from cogs.utils import errors -from cogs.utils.game import Game -from cogs.utils.player import Players -from cogs.utils.pagenator import Pagenator +from application import errors +from application.game import Game +from application.player import Players +from application.pagenator import Pagenator class Vote(commands.Cog): diff --git a/main.py b/main.py index 9c65bf1..b25dc65 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,8 @@ import os import traceback from discord.ext import commands -from cogs.utils.game import Game -from cogs.utils.errors import PermissionNotFound, NotGuildChannel, NotDMChannel +from application.game import Game +from application.errors import PermissionNotFound, NotGuildChannel, NotDMChannel bot = commands.Bot(command_prefix='/') bot.game = Game() diff --git a/tests/test_game.py b/tests/test_game.py index a4631b8..f480023 100644 --- a/tests/test_game.py +++ b/tests/test_game.py @@ -1,6 +1,6 @@ import pytest -from cogs.utils.game import Game -from cogs.utils.player import Player +from application.game import Game +from application.player import Player player1 = Player(1) player2 = Player(2) From dc158598c8977e4c5b73de022aa3780d02191f65 Mon Sep 17 00:00:00 2001 From: shun <1ntegrale9uation@gmail.com> Date: Tue, 3 Oct 2023 16:09:07 +0900 Subject: [PATCH 3/5] =?UTF-8?q?constants=E3=83=87=E3=82=A3=E3=83=AC?= =?UTF-8?q?=E3=82=AF=E3=83=88=E3=83=AA=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/roles.py | 27 --------------------------- constants/__init__.py | 0 constants/roles.py | 24 ++++++++++++++++++++++++ 3 files changed, 24 insertions(+), 27 deletions(-) delete mode 100644 application/roles.py create mode 100644 constants/__init__.py create mode 100644 constants/roles.py diff --git a/application/roles.py b/application/roles.py deleted file mode 100644 index 9001a21..0000000 --- a/application/roles.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -役職編成の辞書 - -村: 村人 -狼: 人狼 -占: 占い師 -""" - -# シンプル編成(仮) -simple = { - 1: '村', - 2: '村狼', - 3: '村村狼', - 4: '村狼占狼', - 5: '村村狼占狼', - 6: '村村村狼占狼', - 7: '村村村村狼占狼', - 8: '村村村村狼狼占村', - 9: '村村村村村狼狼占村', - 10: '村村村村狼狼占狼村村', - 11: '村村村村村狼狼占狼村村', - 12: '村村村村村村狼狼占狼村村', - 13: '村村村村村村村狼狼占狼村村', - 14: '村村村村村村村村狼狼占狼村村', - 15: '村村村村村村村村村狼狼占狼村村', - 16: '村村村村村村村狼狼狼占狼村村村村', -} diff --git a/constants/__init__.py b/constants/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/constants/roles.py b/constants/roles.py new file mode 100644 index 0000000..0ef5900 --- /dev/null +++ b/constants/roles.py @@ -0,0 +1,24 @@ +""" +役職編成の辞書 + +村: 村人 +狼: 人狼 +占: 占い師 +""" + +# シンプル編成(仮) +simple = { + 4: '村村狼占', + 5: '村村狼狼占', + 6: '村村村狼狼占', + 7: '村村村村狼狼占', + 8: '村村村村村狼狼占', + 9: '村村村村村村狼狼占', + 10: '村村村村村村狼狼狼占', + 11: '村村村村村村村狼狼狼占', + 12: '村村村村村村村村狼狼狼占', + 13: '村村村村村村村村村狼狼狼占', + 14: '村村村村村村村村村村狼狼狼占', + 15: '村村村村村村村村村村村狼狼狼占', + 16: '村村村村村村村村村村村狼狼狼狼占', +} 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 4/5] =?UTF-8?q?extensions=E5=91=A8=E3=82=8A=E3=81=AE?= =?UTF-8?q?=E4=BB=95=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 From 06989b4a9fdacb57a5083c67fd0ad779122fad3c Mon Sep 17 00:00:00 2001 From: shun <1ntegrale9uation@gmail.com> Date: Tue, 3 Oct 2023 18:37:50 +0900 Subject: [PATCH 5/5] update LICENSE --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 87d9761..f9ed8f3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019-2020 Discord Bot Portal JP +Copyright (c) 2019-2023 Discord Bot Portal JP Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal