Commit bec41e9b authored by Ferdinand Schober's avatar Ferdinand Schober 💬
Browse files

adding a number system for quicker userinputs

parent 56755ad0
from discord.ext import commands
from discord import Permissions, PermissionOverwrite, Member, utils, Colour
from discord.ext.commands import CommandInvokeError, CommandError
from asyncio import create_task, Event, Lock, gather
from functools import wraps
from ..roles import Amor, Girl, Hunter, Seer, Villager, Werewolf, Whore, Witch, Role
from import Game
def delete_command_message(command):
async def wrapper(self, ctx, *args, **kwargs):
await command(self, ctx, *args, **kwargs)
await ctx.message.delete()
return wrapper
def is_alive(ctx):
if not in[ctx.guild].alive:
raise CommandError('You must be alive to invoke this command!')
return True
def is_not_alive(ctx):
if in[ctx.guild].alive:
raise CommandError('You must be dead to invoke this command!')
return True
def args_alive(command):
async def wrapper(self, ctx, *args, **kwargs):
for arg in args:
if type(arg) == Member and arg not in[ctx.guild].alive:
raise CommandError(f'Member {arg.mention} lebt nicht mehr!')
for arg in kwargs.values():
if type(arg) == Member and arg not in[ctx.guild].alive:
raise CommandError(f'Member {arg.mention} lebt nicht mehr!')
await command(self, ctx, *args, **kwargs)
return wrapper
def is_role(Role):
def is_role(ctx):
if[ctx.guild].role_of_type.get(Role) == None:
raise CommandError(f'Die Rolle "{Role.get_name()}" ist nicht im Spiel!')
if not in[ctx.guild].role_of_type[Role].members:
raise CommandError(f'Deine Rolle ist nicht {Role.get_name()}, {}!')
return True
return is_role
def is_running(ctx):
if not ctx.cog.running[ctx.guild]:
raise CommandInvokeError('Game ist not running')
return True
class WerwolfCommands(commands.Cog):
def __init__(self, client):
self.client = client
self.init_event = Event() = dict()
self.running = dict()
self.lock = Lock()
async def on_ready(self):
for guild in self.client.guilds:[guild] = None
self.running[guild] = False
async def create_channels(self, guild, channel_names, role_alive, role_dead):
if not self.lock.locked():
async with self.lock:
overwrites = {
role_alive : PermissionOverwrite(read_messages=False, send_messages=False),
role_dead : PermissionOverwrite(read_messages=True, send_messages=False),
ww_category = await guild.create_category('werwolf')
await gather(*[ww_category.create_text_channel(name, overwrites=overwrites) for name in channel_names])
await ww_category.create_voice_channel('talk')
dead_talk = await ww_category.create_voice_channel('dead')
await dead_talk.set_permissions(role_alive, connect=False)
await utils.get(ww_category.text_channels, name='dorf').set_permissions(role_alive, read_messages=True, send_messages=True)
return ww_category
async def clear_channels(self, ctx):
await self.uninstall(ctx.guild)
async def uninstall(self, guild):
if not self.lock.locked():
async with self.lock:
ww_category = utils.get(guild.categories, name='werwolf')
if not ww_category: return
await gather(*[channel.delete() for channel in ww_category.channels])
await ww_category.delete()
async def start(self, ctx):
async with
await self.init_event.wait()
if self.running[ctx.guild]:
await ctx.send('Spiel läuft bereits!')
async with
self.running[ctx.guild] = True
lobby = utils.get(ctx.guild.channels, name='lobby')
members = lobby.members
role_alive = utils.get(ctx.guild.roles, name='lebendig')
role_dead = utils.get(ctx.guild.roles, name='tot')
if not role_alive:
role_alive = await ctx.guild.create_role(name='lebendig',, hoist=True, mentionable=True)
if not role_dead:
role_dead = await ctx.guild.create_role(name='tot',, hoist=True, mentionable=True)
#roles = [Girl, Werewolf, Hunter, Witch, Whore, Seer]
roles = [Werewolf, Hunter, Witch, Whore, Amor, Werewolf, Girl, Seer, Villager, Werewolf]
category = await self.create_channels(ctx.guild, ['info', 'dorf'] + [role.get_channel_name() for role in set(roles[:len(members)]) ] + ['liebespaar'], role_alive, role_dead)
#await gather(*[member.move_to(category.voice_channels[0]) for member in members])[ctx.guild] = Game(category, members, roles, role_alive, role_dead)
self.running[ctx.guild] = False
#await gather(*[member.move_to(lobby) for member in members])
async def on_command_error(self, ctx, error):
await ctx.send(str(error))
async def on_message(self, message):
game =[message.guild]
if not game:
role = game.role_of_member.get(
if not role:
if not in
await role.command(, message.content)
def setup(client):
import itertools
import random
from asyncio import Event, Lock
from abc import ABC, abstractmethod
from discord import Embed
from discord import utils
def distribute_roles(game, members, roles):
members_for_role = { role : set() for role in roles[:len(members)] }
for member, role in zip(members, roles):
roles = [ role(game, members_for_role[role]) for role in members_for_role.keys() ]
return { member : role for role in roles for member in role.members }
class Role(ABC):
def get_name(cls): return None
def get_channel_name(cls): return None
def get_team(cls): return None
def get_max_members(cls): return None
def get_command_help_embed(cls): return None
def info(self):
def __init__(self, game, members):
if self.get_max_members() != None and len(members) > self.get_max_members():
raise Exception(f'There can only be {self.get_max_members()} members with the role {self.get_name()}') = game
self.members = set(members) = utils.get(game.category.text_channels, name=self.get_channel_name())
if not
raise Exception(f'Channel {self.get_channel_name()} not found')
self.event = Event()
self.lock = Lock()
async def on_kill(self, member): pass
async def on_night_begin(self):
await'Die {}te Nacht Beginnt...')
async def help(self):
async def task(self):
await self.task_()
async def command(cls, message, member):
async def task_(self): pass
async def on_night_end(self): pass
async def wait(self):
await self.event.wait()
async def wait_for(self, role):
role =
if role:
await role.wait()
def notify(self):
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment