Skip to content

Commit

Permalink
extensions周りの仕様をdpy v2に追従
Browse files Browse the repository at this point in the history
  • Loading branch information
1ntegrale9 committed Oct 3, 2023
1 parent dc15859 commit 218e03c
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 51 deletions.
9 changes: 0 additions & 9 deletions application/errors.py

This file was deleted.

6 changes: 6 additions & 0 deletions constants/discord.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from dotenv import load_dotenv
from os import getenv

load_dotenv()

TOKEN = getenv('DISCORD_BOT_TOKEN')
Empty file added extensions/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions extensions/handle_error.py
Original file line number Diff line number Diff line change
@@ -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))
4 changes: 2 additions & 2 deletions cogs/players.py → extensions/players.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
8 changes: 4 additions & 4 deletions cogs/status.py → extensions/status.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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))
8 changes: 4 additions & 4 deletions cogs/vote.py → extensions/vote.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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))
54 changes: 22 additions & 32 deletions main.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
discord.py[voice]>=2.3.1
dotenv
Empty file added utils/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions utils/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import discord

class PermissionNotFound(discord.DiscordException):
pass


class NotGuildChannel(discord.DiscordException):
pass


class NotDMChannel(discord.DiscordException):
pass

0 comments on commit 218e03c

Please sign in to comment.