commands instead of on_message

This commit is contained in:
Bibin Muttappillil 2020-04-12 23:01:47 +02:00
parent eaf1686c5d
commit 7495336269
4 changed files with 79 additions and 69 deletions

View file

@ -13,7 +13,8 @@ if TOKEN is None:
print("Missing discord token!")
exit(1)
bot = commands.Bot(command_prefix=commands.when_mentioned_or('$werewolf '))
PREFIX = '$w '
bot = commands.Bot(command_prefix=commands.when_mentioned_or(PREFIX))
@bot.event
@ -22,34 +23,70 @@ async def on_ready():
print('We have logged in as {0.user}'.format(bot))
@bot.event
async def on_message(message):
if message.author == bot.user:
return
await bot.process_commands(message)
# game commands
game_instances = {}
@bot.group(help="werewolf game", description="Prefix for the One Night Ultimate Werewolf commands.")
async def game(ctx):
if ctx.invoked_subcommand is None:
await bot.say('Invalid sub command passed...')
await ctx.send('Invalid sub command passed...')
@game.command(help="start werewolf game", description="Start One Night Ultimate Werewolf game.")
async def setup(ctx):
if ctx.channel in game_instances:
await ctx.send("Game already setup in this channel")
else:
game_instances[ctx.channel] = Werewolf_Game(bot, ctx.channel)
def game_running(command):
@functools.wraps(command)
async def wrapper(ctx):
if ctx.channel not in game_instances:
await ctx.send(f"No game setup yet. Use {PREFIX}game setup")
elif game_instances[ctx.channel].running:
await ctx.send("Sorry! A game is already running")
else:
await command(ctx)
return wrapper
@game.command(help="start a round", description="Play one round of One Night Ultimate Werewolf")
@game_running
async def start(ctx):
if werewolf_game.running:
await ctx.message.channel.send("Sorry! A game is already running")
return
await game_instances[ctx.channel].game()
werewolf_game.running = True
werewolf_game.set_channel(ctx.message.channel)
await werewolf_game.game()
@game.command(help="set players", description="Set the players for the next round.")
@game_running
async def players(ctx):
await game_instances[ctx.channel].set_players(ctx.message)
@game.command(help="set roles", description="Set the roles for the next round.")
@game_running
async def roles(ctx):
try:
await game_instances[ctx.channel].set_roles(ctx.message.content.split()[3:])
except ValueError as error:
await ctx.send(error)
# ONLY FOR TESTING
@game.command()
@game_running
async def vote(ctx):
await game_instances[ctx.channel].vote()
# smaller commands
@bot.command(help="greets you", description="This just says hello back to the message author.")
async def hello(ctx):
await ctx.message.channel.send(f"Hello {ctx.message.author.name} :wave:")
await ctx.send(f"Hello {ctx.message.author.name} :wave:")
@bot.command(help="test bot responsiveness", description="This is a debug function to see if the bot listens to a command.")
@ -58,11 +95,14 @@ async def ping(ctx):
await ctx.send("pong")
# developer commands
def developer(command):
@functools.wraps(command)
async def wrapper(ctx):
DEV_ID = 461892912821698562
if ctx.message.author.id == DEV_ID:
if ctx.author.id == DEV_ID:
await command(ctx)
else:
await ctx.send("This command is not for you!")
@ -81,6 +121,4 @@ async def debug(ctx):
print("DEBUG")
print(ctx.message.author.id)
werewolf_game = Werewolf_Game(bot)
bot.run(TOKEN)