Skip to content

Commit

Permalink
Merge pull request #28 from DiscordBotPortalJP/refactor
Browse files Browse the repository at this point in the history
refactor
  • Loading branch information
1ntegrale9 authored Oct 3, 2023
2 parents 1913d99 + 06989b4 commit 00622a8
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 101 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
discordbot: python discordbot.py
web: python main.py
Empty file added application/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion cogs/utils/game.py → application/game.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Optional
from cogs.utils.player import Player, Players
from application.player import Player, Players


class Game():
Expand Down
File renamed without changes.
File renamed without changes.
9 changes: 0 additions & 9 deletions cogs/utils/errors.py

This file was deleted.

27 changes: 0 additions & 27 deletions cogs/utils/roles.py

This file was deleted.

Empty file added constants/__init__.py
Empty file.
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')
24 changes: 24 additions & 0 deletions constants/roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
役職編成の辞書
村: 村人
狼: 人狼
占: 占い師
"""

# シンプル編成(仮)
simple = {
4: '村村狼占',
5: '村村狼狼占',
6: '村村村狼狼占',
7: '村村村村狼狼占',
8: '村村村村村狼狼占',
9: '村村村村村村狼狼占',
10: '村村村村村村狼狼狼占',
11: '村村村村村村村狼狼狼占',
12: '村村村村村村村村狼狼狼占',
13: '村村村村村村村村村狼狼狼占',
14: '村村村村村村村村村村狼狼狼占',
15: '村村村村村村村村村村村狼狼狼占',
16: '村村村村村村村村村村村狼狼狼狼占',
}
44 changes: 0 additions & 44 deletions discordbot.py

This file was deleted.

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))
6 changes: 3 additions & 3 deletions cogs/players.py → extensions/players.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from discord.ext import commands
from cogs.utils.player import Player
from application.player import Player


class PlayersCog(commands.Cog):
Expand Down 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 cogs.utils.roles import simple
from cogs.utils.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))
14 changes: 7 additions & 7 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 cogs.utils import errors
from cogs.utils.game import Game
from cogs.utils.player import Players
from cogs.utils.pagenator import Pagenator
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))
34 changes: 34 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import discord
from discord.ext import commands
from application.game import Game
from constants.discord import TOKEN

extensions = [
'handle_error',
'players',
'status',
'vote',
]


class WerewolfBot(commands.Bot):
def __init__(self):
super().__init__(
command_prefix=commands.when_mentioned_or('$'),
intents=discord.Intents.all(),
)

async def setup_hook(self):
for extension in extensions:
await self.load_extension(f'extensions.{extension}')
# await self.tree.sync()


def main():
bot = WerewolfBot()
bot.game = Game()
bot.run(TOKEN)


if __name__ == '__main__':
main()
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
discord.py[voice]>=1.4.1
discord.py[voice]>=2.3.1
dotenv
2 changes: 1 addition & 1 deletion runtime.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-3.8.5
python-3.11.6
4 changes: 2 additions & 2 deletions tests/test_game.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
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 00622a8

Please sign in to comment.