async def handle_event_in_all_guilds(guild: Optional[Guild], event: Event, *args, **kwargs): """Handle the Discord event in all listeners of the concerned guilds.""" await asyncio.gather( *(handle_event_in_all_listeners(guild_wrapper, event, *args, **kwargs) for guild_wrapper in GuildManager().values() if guild_wrapper == guild or guild is None))
async def _on_channel_victory(self, channel): self._channels[channel].success = True await channel.send(self._messages["VICTORY"], file=File(self._messages["VICTORY_FILE"])) await channel.send(self._messages["FIRST_REACTION"]) await asyncio.sleep(4) if self._simple_mode: await channel.send(self._messages["COMMENT_SIMPLE"]) else: role_message = await channel.send(self._messages["COMMENT"].format( nb_roles_per_player=self._max_number_of_reactions_per_user, nb_players_per_role=self._max_users_with_role)) manager = RoleByReactionManager.get(self.guild) options = RoleMenuOptions( required_roles=[self._visitor_d], max_users_with_role=self._max_users_with_role * GuildManager().get_guild(self.guild).nb_teams, max_number_of_reactions_per_user=self. _max_number_of_reactions_per_user, remove_role_on_reaction_removal=True, allow_role_change=self._allow_role_change, ) await manager.add(role_message, menu=self._characters_menu, options=options) await asyncio.sleep(20) await channel.edit(send_messages=False)
async def on_guild_join(guild): logger.info(f"Bot joined a new guild: {guild}") await GuildManager().add_guild(guild, versions=GAME_LANGUAGE) # In production mode: ensure the bot stays awake global BOT_STAYING_AWAKE if not BOT_STAYING_AWAKE and not DEBUG_MODE: for guild_wrapper in GuildManager().values( ): # do it for the first guild found logger.info("Starting awakening coroutine...") BOT_STAYING_AWAKE = True return await stay_awake(guild_wrapper, AWAKE_REFRESH_PERIOD)
async def on_ready(): logger.info(f"Bot ready!") global BOT_INITIALIZED if not BOT_INITIALIZED: BOT_INITIALIZED = True await init_bot() await handle_event_in_all_guilds(None, Event.READY) # In production mode: ensure the bot stays awake global BOT_STAYING_AWAKE if not BOT_STAYING_AWAKE and not DEBUG_MODE: logger.info("Starting awakening coroutine...") for guild_wrapper in GuildManager().values( ): # do it for the first guild found BOT_STAYING_AWAKE = True return await stay_awake(guild_wrapper, AWAKE_REFRESH_PERIOD)
async def change_channel_number(self, message): # For game master only if self._step >= 4: await message.channel.send("Too late to change number of teams :/") else: try: args = list(filter(None, message.content.split(" "))) nb_teams, max_number_per_team = int(args[1]), int(args[2]) if nb_teams < 1 or nb_teams > 3 or max_number_per_team < 1: raise ValueError(f"Bad interval for values {nb_teams} {max_number_per_team}") self._nb_teams, self._max_number_per_team = nb_teams, max_number_per_team GuildManager().get_guild(self.guild).nb_teams = nb_teams # Change number of teams globally. except (ValueError, IndexError) as err: err_msg = f"Error to convert command to team number: {message.content} (Error: {err})" logger.warning(err_msg) await message.channel.send(err_msg) else: await message.channel.send(f"Changed number of teams to {self._nb_teams} with a maximum " f"of {self._max_number_per_team} members per team")
def main(): logger.info("Bot starting") # Init singleton GuildManager _is_ok = GuildManager().set_bot(BOT, init_guild, max_guilds=MAX_GUILDS, max_pending_guilds=MAX_PENDING_GUILDS) if not _is_ok: logger.critical("Setting bot in GuildManager failed!") # Run the bot try: logger.info("Bot entering run loop...") BOT.run(_TOKEN) except (ClientConnectorError, discord.errors.HTTPException) as err: logger.error(f"Connection error, failed to connect: {err}") logger.exception(err) except Exception as err: logger.critical(f"Fatal error: {err}") logger.exception(err) logger.info("Bot stopped")
async def _init(self): self._step = 0 self._counter_2_to_3 = 0 self._rolemenu_manager = None self._rules_msg_id = None self._choose_team_msg = None self._nb_players = None GuildManager().get_guild(self.guild).nb_teams = self._nb_teams # Change number of teams globally. # Update channel permissions to forbid send messages and read message history permissions = PermissionOverwriteDescription(read_message_history=True, add_reactions=False, send_messages=False, view_channel=True) master_perm = PermissionOverwriteDescription(read_message_history=True, add_reactions=True, send_messages=True, view_channel=True) overwrites = {self._default_role_description.object_reference: PermissionOverwrite(**permissions.to_dict()), self._master_role_description.object_reference: PermissionOverwrite(**master_perm.to_dict()), } await self._introduction_channel_d.object_reference.edit(overwrites=overwrites) # Send a welcome message to pre-existing users for member in self._introduction_channel_d.object_reference.members: if (self._default_role_description.has_the_role(member) and not self._master_role_description.has_the_role(member)): await self.welcome_dm_message(member, origin_channel=self._support_channel_d.object_reference) # Purge introduction channel and send the first message intro_channel: TextChannel = self._introduction_channel_d.object_reference while await intro_channel.history(limit=1).flatten(): # completely purge the channel await self._introduction_channel_d.object_reference.purge(limit=1000) await self.welcome_general_message(self._introduction_channel_d.object_reference) # Send instructions to master channel msg = self._messages["INTRO_MASTER"].format(emoji_1=self._messages["EMOJI_1"], emoji_2=self._messages["EMOJI_2"], key_words=self._messages["TRIGGERS_2"], emoji_3=self._messages["EMOJI_3"], emoji_4=self._messages["EMOJI_4"], emoji_5=self._messages["EMOJI_5"], change_teams_command=f"`{self._nb_teams_command} " f"NUMBER_TEAMS MAX_NUMBER_PER_TEAM`") await long_send(self._master_channel_description.object_reference, msg, quotes=False) music_msg = await long_send(self._music_channel_description.object_reference, self._messages["JINGLES"]) await MusicTools.jingle_palette_from_message(music_msg) return True