on_message -> commands

This commit is contained in:
Bibin Muttappillil 2020-04-03 16:09:39 +02:00
parent 534013a7dc
commit eaf1686c5d
7 changed files with 93 additions and 66 deletions

View File

@ -1,8 +1,9 @@
import os import os
import discord
import asyncio
from dotenv import load_dotenv from dotenv import load_dotenv
import functools
import asyncio
import discord
from discord.ext import commands
from werewolf_game import Game as Werewolf_Game from werewolf_game import Game as Werewolf_Game
@ -12,54 +13,74 @@ if TOKEN is None:
print("Missing discord token!") print("Missing discord token!")
exit(1) exit(1)
bot = discord.Client() bot = commands.Bot(command_prefix=commands.when_mentioned_or('$werewolf '))
@bot.event @bot.event
async def on_ready(): async def on_ready():
await bot.change_presence(status=discord.Status.idle, activity=discord.Game('One Night Ultimate Werewolf'))
print('We have logged in as {0.user}'.format(bot)) print('We have logged in as {0.user}'.format(bot))
async def hello(message):
print("Hello")
await message.channel.send('Hello!:regional_indicator_a:')
print(message.mentions)
@bot.event @bot.event
async def on_message(message): async def on_message(message):
global running
if message.author == bot.user: if message.author == bot.user:
return return
await bot.process_commands(message)
if message.content.startswith('$hello'): @bot.group(help="werewolf game", description="Prefix for the One Night Ultimate Werewolf commands.")
await hello(message) async def game(ctx):
return if ctx.invoked_subcommand is None:
await bot.say('Invalid sub command passed...')
if message.content.startswith('$logout'): @game.command(help="start werewolf game", description="Start One Night Ultimate Werewolf game.")
await bot.logout() async def start(ctx):
return
if message.content.startswith('$werewolf'):
# start (only one instance running)
if werewolf_game.running: if werewolf_game.running:
await message.channel.send("Sorry! A game is already running") await ctx.message.channel.send("Sorry! A game is already running")
return return
werewolf_game.running = True werewolf_game.running = True
werewolf_game.set_channel(message.channel) werewolf_game.set_channel(ctx.message.channel)
await werewolf_game.game() await werewolf_game.game()
return
@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:")
@bot.command(help="test bot responsiveness", description="This is a debug function to see if the bot listens to a command.")
async def ping(ctx):
print("pong")
await ctx.send("pong")
def developer(command):
@functools.wraps(command)
async def wrapper(ctx):
DEV_ID = 461892912821698562
if ctx.message.author.id == DEV_ID:
await command(ctx)
else:
await ctx.send("This command is not for you!")
return wrapper
@bot.command(help="not for you", description="Shut down the bot.")
@developer
async def logout(ctx):
await bot.logout()
@bot.command(help="debug")
@developer
async def debug(ctx):
print("DEBUG")
print(ctx.message.author.id)
werewolf_game = Werewolf_Game(bot) werewolf_game = Werewolf_Game(bot)
bot.run(TOKEN) bot.run(TOKEN)

View File

@ -1,8 +1,9 @@
from random import shuffle from random import shuffle
import asyncio import asyncio
from werewolf_roles import Role, Doppelganger, Werewolf, Minion, Mason, Seer, Robber, Troublemaker, Drunk, Insomniac, Villiager, Tanner, Hunter, No_role from werewolf_roles import Role, Werewolf, Minion, Tanner, Hunter
from werewolf_players import Player, No_player from werewolf_players import Player, No_player
class Game: class Game:
def __init__(self, bot): def __init__(self, bot):
@ -11,26 +12,22 @@ class Game:
self.player_list = [] self.player_list = []
self.role_list = Role.role_set self.role_list = Role.role_set
def set_channel(self, channel): def set_channel(self, channel):
self.channel = channel self.channel = channel
async def send(self, message): async def send(self, message):
await self.channel.send(message) await self.channel.send(message)
async def receive(self, command): async def receive(self, command):
def check(msg): def check(msg):
return msg.channel == self.channel and msg.content.startswith(command) return msg.channel == self.channel and msg.content.startswith(command)
return await self.bot.wait_for('message', check = check) return await self.bot.wait_for('message', check=check)
def setup(self): def setup(self):
self.werewolf_list = [] self.werewolf_list = []
self.mason_list = [] self.mason_list = []
async def set_players(self): async def set_players(self):
await self.send("Who is playing?") await self.send("Who is playing?")
@ -47,7 +44,6 @@ class Game:
# send confirmation # send confirmation
await self.send("Players: " + ", ".join(p.name() for p in self.player_list)) await self.send("Players: " + ", ".join(p.name() for p in self.player_list))
async def set_roles(self): async def set_roles(self):
await self.send("With which roles do you want to play?") await self.send("With which roles do you want to play?")
msg = await self.receive('$roles') msg = await self.receive('$roles')
@ -69,7 +65,6 @@ class Game:
# send confirmation # send confirmation
await self.send("Roles: " + ", ".join(r.name() for r in self.role_list)) await self.send("Roles: " + ", ".join(r.name() for r in self.role_list))
def distribute_roles(self): def distribute_roles(self):
shuffle(self.role_list) shuffle(self.role_list)
for i in range(len(self.player_list)): for i in range(len(self.player_list)):
@ -77,21 +72,20 @@ class Game:
self.role_list[i].setPlayer(self.player_list[i]) self.role_list[i].setPlayer(self.player_list[i])
self.middle_card = self.role_list[-3:] self.middle_card = self.role_list[-3:]
self.active_role = sorted(self.role_list[:-3], key = lambda x: x.order) #necessary? self.active_role = sorted(self.role_list[:-3], key=lambda x: x.order)
async def start_night(self): async def start_night(self):
await asyncio.gather( *[p.send("The night has begun") for p in self.player_list] ) await asyncio.gather(*[p.send("The night has begun") for p in self.player_list])
async def send_role(self): async def send_role(self):
await asyncio.gather( *[p.send("Your role: " + p.night_role.name()) for p in self.player_list] ) await asyncio.gather(*[p.send("Your role: " + p.night_role.name()) for p in self.player_list])
async def night_phases(self): async def night_phases(self):
await asyncio.gather( *[r.phase1() for r in self.active_role] ) await asyncio.gather(*[r.phase1() for r in self.active_role])
await asyncio.gather( *[r.phase2() for r in self.active_role] ) await asyncio.gather(*[r.phase2() for r in self.active_role])
await asyncio.gather( *[r.phase3() for r in self.active_role] ) await asyncio.gather(*[r.phase3() for r in self.active_role])
await asyncio.gather( *[r.phase4() for r in self.active_role] ) await asyncio.gather(*[r.phase4() for r in self.active_role])
await asyncio.gather( *[r.phase5() for r in self.active_role] ) await asyncio.gather(*[r.phase5() for r in self.active_role])
async def start_day(self): async def start_day(self):
await self.send("The day has started") await self.send("The day has started")
@ -102,7 +96,7 @@ class Game:
await self.receive('$vote') await self.receive('$vote')
await self.send("Vote in DM") await self.send("Vote in DM")
await asyncio.gather( *[p.cast_vote(options) for p in self.player_list] ) await asyncio.gather(*[p.cast_vote(options) for p in self.player_list])
await self.send("Votes\n\n" + '\n'.join(str(p) + " :arrow_right: " + str(p.vote) for p in self.player_list)) await self.send("Votes\n\n" + '\n'.join(str(p) + " :arrow_right: " + str(p.vote) for p in self.player_list))
@ -187,7 +181,6 @@ class Game:
def end(self): def end(self):
self.running = False self.running = False
async def game(self): async def game(self):
try: try:
@ -202,7 +195,7 @@ class Game:
await self.night_phases() await self.night_phases()
await self.start_day() await self.start_day()
#discussion timer # discussion timer
options = self.player_list + [No_player(self)] options = self.player_list + [No_player(self)]
await self.vote(options) await self.vote(options)

View File

@ -1,3 +1,6 @@
from werewolf_roles import No_role
class Player: class Player:
@staticmethod @staticmethod
@ -8,9 +11,8 @@ class Player:
p.game = game p.game = game
return p return p
@staticmethod def swap(self, player_B):
def swap(player_A, player_B): self.day_role, player_B.day_role = player_B.day_role, self.day_role
player_A.day_role, player_B.day_role = player_B.day_role, player_A.day_role
def setRole(self, role): def setRole(self, role):
self.night_role = role self.night_role = role
@ -29,13 +31,13 @@ class Player:
await self.dm.send(message) await self.dm.send(message)
async def ask_choice(self, options): async def ask_choice(self, options):
await self.send('\n'.join( "(" + str(i) + ") " + str(options[i]) for i in range(len(options)) )) await self.send('\n'.join("(" + str(i) + ") " + str(options[i]) for i in range(len(options))))
async def receive_choice(self, options): async def receive_choice(self, options):
def check(choice): def check(choice):
return choice.channel == self.dm and choice.content.isdigit() and 0 <= int(choice.content) < len(options) return choice.channel == self.dm and choice.content.isdigit() and 0 <= int(choice.content) < len(options)
return int((await self.game.bot.wait_for('message', timeout=30.0, check = check)).content) return int((await self.game.bot.wait_for('message', timeout=30.0, check=check)).content)
async def get_choice(self, options): async def get_choice(self, options):
await self.ask_choice(options) await self.ask_choice(options)
@ -44,6 +46,7 @@ class Player:
async def cast_vote(self, options): async def cast_vote(self, options):
self.vote = options[await self.get_choice(options)] self.vote = options[await self.get_choice(options)]
class No_player(Player): class No_player(Player):
def __init__(self): def __init__(self):

View File

@ -35,9 +35,11 @@ class Role:
def __str__(self): def __str__(self):
return self.name() return self.name()
class Doppelganger(Role): class Doppelganger(Role):
order = 1 order = 1
class Werewolf(Role): class Werewolf(Role):
order = 2 order = 2
@ -107,9 +109,10 @@ class Robber(Role):
self.choice = await self.player.get_choice(self.player.other()) self.choice = await self.player.get_choice(self.player.other())
async def phase3(self): async def phase3(self):
Player.swap(self.player, self.player.other()[self.choice]) self.player.swap(self.player.other()[self.choice])
await self.player.send("You robbed: " + str(self.player.day_role)) await self.player.send("You robbed: " + str(self.player.day_role))
class Troublemaker(Role): class Troublemaker(Role):
order = 7 order = 7
@ -119,10 +122,11 @@ class Troublemaker(Role):
self.B = await self.player.get_choice(self.player.other()) self.B = await self.player.get_choice(self.player.other())
async def phase4(self): async def phase4(self):
Player.swap(self.player.other()[self.A], self.player.other()[self.B]) self.player.other()[self.A].swap(self.player.other()[self.B])
# receive conformation # receive conformation
await self.player.send("Received " + str(self.A) + " " + str(self.B)) await self.player.send("Received " + str(self.A) + " " + str(self.B))
class Drunk(Role): class Drunk(Role):
order = 8 order = 8
@ -132,25 +136,31 @@ class Drunk(Role):
async def phase5(self): async def phase5(self):
self.player.day_role, self.game.middle_card[self.choice] = self.game.middle_card[self.choice], self.player.day_role self.player.day_role, self.game.middle_card[self.choice] = self.game.middle_card[self.choice], self.player.day_role
#receive conformation # receive conformation
await self.player.send("Received " + str(self.choice)) await self.player.send("Received " + str(self.choice))
class Insomniac(Role): class Insomniac(Role):
order = 9 order = 9
async def phase5(self): async def phase5(self):
await self.player.send("You are now: " + str(self.player.day_role)) await self.player.send("You are now: " + str(self.player.day_role))
class Villiager(Role): class Villiager(Role):
order = 10 order = 10
class Tanner(Role): class Tanner(Role):
order = 11 order = 11
class Hunter(Role): class Hunter(Role):
order = 12 order = 12
class No_role(Role): class No_role(Role):
order = 1000 order = 1000
Role.role_set = [Doppelganger, Werewolf, Minion, Mason, Seer, Robber, Troublemaker, Drunk, Insomniac, Villiager, Tanner, Hunter] Role.role_set = [Doppelganger, Werewolf, Minion, Mason, Seer, Robber, Troublemaker, Drunk, Insomniac, Villiager, Tanner, Hunter]