more refactoring

This commit is contained in:
Bibin Muttappillil 2020-07-15 16:09:38 +02:00
parent b85c20b65a
commit 13064a3af0
8 changed files with 51 additions and 40 deletions

View File

@ -13,22 +13,25 @@ class Developer(commands.Cog):
await self.bot.change_presence(status=discord.Status.online, activity=discord.Game('One Night Ultimate Werewolf')) await self.bot.change_presence(status=discord.Status.online, activity=discord.Game('One Night Ultimate Werewolf'))
print('We have logged in as {0.user}'.format(self.bot)) print('We have logged in as {0.user}'.format(self.bot))
@commands.command()
async def hello(self, ctx):
await ctx.send(f"Hello {ctx.message.author.name} :wave:")
@commands.command()
async def ping(self, ctx):
print("pong", self.bot.owner_id, await self.bot.application_info())
print(await self.bot.is_owner(ctx.author))
await ctx.send("pong")
async def is_dev(ctx): async def is_dev(ctx):
if ctx.author.id == 461892912821698562: if ctx.author.id == 461892912821698562:
return True return True
await ctx.send("This command is not for you!") await ctx.send("This command is not for you!")
return False return False
@commands.command()
async def hello(self, ctx):
await ctx.send(f"Hello {ctx.message.author.name} :wave:")
@commands.group(name="gg")
async def group(self, ctx):
pass
@group.command()
@commands.check(is_dev)
async def ping(self, ctx):
await ctx.send("pong")
@commands.command() @commands.command()
@commands.check(is_dev) @commands.check(is_dev)
async def logout(self, ctx): async def logout(self, ctx):

View File

@ -1,25 +1,23 @@
"""This (abstract) module is a template for Discord Cog's for game specific channel commands""" """Has a single class: Game_cog"""
# standard library imports # standard library imports
from typing import Dict from typing import Dict, Type
# discord imports # discord imports
import discord import discord
from discord.ext import commands
# local imports # local imports
from ..send_message import Send_message from ..send_message import Send_message
from .game import Game from .game import Game
# TODO: take group as argument to add subcommands class Game_cog(Send_message):
class Game_cog(Send_message, commands.Cog):
"""This (abstract) class is are common function for the Game Cog's (setup-game, pre-game, in-game), mainly has checker functions""" """This (abstract) class is are common function for the Game Cog's (setup-game, pre-game, in-game), mainly has checker functions"""
def __init__(self, bot, game_instances: Dict[discord.TextChannel, Game]): def __init__(self, bot, game_cls: Type[Game]):
self.bot = bot self.bot = bot
self.game_instances = game_instances self.game_cls = game_cls
self.game_instances = Dict[discord.TextChannel, self.game_cls]
async def setup_check(self, ctx): async def setup_check(self, ctx):
if ctx.channel not in self.game_instances: if ctx.channel not in self.game_instances:
@ -36,17 +34,13 @@ class Game_cog(Send_message, commands.Cog):
await self.send_wrong(ctx, "No game is running") await self.send_wrong(ctx, "No game is running")
return self.game_instances[ctx.channel].running return self.game_instances[ctx.channel].running
async def setup(self, ctx):
class Setup_game_cog(Game_cog):
"""This (abstract) class is a template for Discord Cog's for game specific channel commands for setting up the channel"""
async def setup(self, ctx, game: Game):
"""This function creates an game instance for this channel""" """This function creates an game instance for this channel"""
if ctx.channel in self.game_instances: if ctx.channel in self.game_instances:
await self.send_wrong(ctx, f"A game '{game.name}' is already setup in this channel") await self.send_wrong(ctx, f"A game '{self.game_cls.name}' is already setup in this channel")
else: else:
self.game_instances[ctx.channel] = game(self.bot, ctx.channel) self.game_instances[ctx.channel] = self.game_cls(self.bot, ctx.channel)
await self.send_friendly(ctx, f"This channel can now play: {game.name}") await self.send_friendly(ctx, f"This channel can now play: {self.game_cls.name}")
async def reset(self, ctx): async def reset(self, ctx):
"""This function deletes the game instance for this channel""" """This function deletes the game instance for this channel"""
@ -54,10 +48,10 @@ class Setup_game_cog(Game_cog):
del self.game_instances[ctx.channel] del self.game_instances[ctx.channel]
# TODO: better info message # TODO: better info message
async def info(self, ctx, game: Game): async def info(self, ctx):
"""Send information about the subcommands for the game""" """Send information about the subcommands for the game"""
embed = discord.Embed(title="How to play?", description="You will need to set up the game and its information in a channel and start the game there. Afterwards the player mainly interact with the bot in DM.", color=0x00ffff) embed = discord.Embed(title="How to play?", description="You will need to set up the game and its information in a channel and start the game there. Afterwards the player mainly interact with the bot in DM.", color=0x00ffff)
embed.set_author(name=f"With this bot you can play {game.name}") embed.set_author(name=f"With this bot you can play {self.game_cls.name}")
# embed.set_thumbnail(url="https://images-na.ssl-images-amazon.com/images/I/717GrDtFKCL._AC_SL1000_.jpg") # embed.set_thumbnail(url="https://images-na.ssl-images-amazon.com/images/I/717GrDtFKCL._AC_SL1000_.jpg")
embed.add_field(name="$w game setup", value="Make this channel playable.", inline=False) embed.add_field(name="$w game setup", value="Make this channel playable.", inline=False)
embed.add_field(name="$w game players", value="Set mentioned users as players", inline=False) embed.add_field(name="$w game players", value="Set mentioned users as players", inline=False)
@ -66,10 +60,7 @@ class Setup_game_cog(Game_cog):
embed.set_footer(text="Have fun!") embed.set_footer(text="Have fun!")
await ctx.send(embed=embed) await ctx.send(embed=embed)
async def pre_game_check(self, ctx):
class Pre_game_cog(Game_cog):
"""This (abstract) class is a template for Discord Cog's for game specific channel commands for setting up the game rounds"""
async def cog_check(self, ctx):
return self.setup_check(ctx) and self.not_running_check(ctx) return self.setup_check(ctx) and self.not_running_check(ctx)
async def players(self, ctx): async def players(self, ctx):
@ -79,10 +70,7 @@ class Pre_game_cog(Game_cog):
self.game_instances[ctx.channel].game = self.bot.loop.create_task(self.game_instances[ctx.channel].round()) self.game_instances[ctx.channel].game = self.bot.loop.create_task(self.game_instances[ctx.channel].round())
await self.game_instances[ctx.channel].game await self.game_instances[ctx.channel].game
async def in_game_check(self, ctx):
class In_game_goc(Game_cog):
"""This (abstract) class is a template for Discord Cog's for game specific channel commands during the game"""
async def cog_check(self, ctx):
return self.setup_check(ctx) and self.running_check(ctx) return self.setup_check(ctx) and self.running_check(ctx)
async def stop(self, ctx): async def stop(self, ctx):

View File

@ -1,3 +1,5 @@
"""Has a single class: Werewolf_Cog"""
# discord imports # discord imports
from discord.ext import commands from discord.ext import commands
@ -6,12 +8,12 @@ from ..game_cog import Game_cog
from .game import Werewolf_game from .game import Werewolf_game
class Werewolf_cog(Game_cog): class Werewolf_cog(Game_cog, commands.Cog):
"""This singleton class is a Discord Cog for the interaction in the werewolf game""" """This singleton class is a Discord Cog for the interaction in the werewolf game"""
@commands.group(invoke_without_command=True) @commands.group(invoke_without_command=True)
async def werewolf(self, ctx): async def werewolf(self, ctx):
# TODO: isn't there a better way to have a default subcommand? # TODO: isn't there a better way to have a default subcommand? Maybe invoke super().info()?
await ctx.invoke(self.bot.get_command('werewolf info')) await ctx.invoke(self.bot.get_command('werewolf info'))
@werewolf.command() @werewolf.command()
@ -29,6 +31,24 @@ class Werewolf_cog(Game_cog):
"""This function deletes the game instance for this channel""" """This function deletes the game instance for this channel"""
await super().reset(ctx) await super().reset(ctx)
@werewolf.command()
@commands.check(Game_cog.pre_game_check)
async def players(self, ctx):
"""registers all mentioned players for the game"""
await super().players(ctx)
@werewolf.command()
@commands.check(Game_cog.pre_game_check)
async def start(self, ctx):
"""starts a round of werewolf"""
await super().start(ctx)
@werewolf.command()
@commands.check(Game_cog.in_game_check)
async def stop(self, ctx):
"""aborts the current round of werewolf"""
await super().stop(ctx)
def setup(bot): def setup(bot):
bot.add_cog(Werewolf_cog(bot, None)) bot.add_cog(Werewolf_cog(bot, Werewolf_game))

View File

@ -31,7 +31,7 @@ bot.load_extension('package.games.werewolf.cog')
@bot.command() @bot.command()
@commands.is_owner() @commands.is_owner()
async def reload(ctx, extension): async def reload(ctx, extension):
bot.reload_extension(f'{extension}') bot.reload_extension(f'package.{extension}')
# checker annotations # checker annotations