def setup_guild_member_list(guild, override=False): global timer_member, _guild_member_list if bool(_guild_member_list): elapsed_time = time() - timer_member if elapsed_time < 86400 and not override: return FileLogger.info(f'Setting up members in {guild.name}') _guild_member_list = {} for i in range(0, len(guild.members)): valid = False for role in guild.members[i].roles: if role.name == '隊員': valid = True break if not valid: continue nick = guild.members[i].nick display_name = guild.members[i].display_name if nick: _guild_member_list[guild.members[i].id] = nick elif display_name: _guild_member_list[guild.members[i].id] = display_name else: _guild_member_list[guild.members[i].id] = guild.members[i].name timer_member = time()
def setup_guild_channel_list(guild, override=False): global timer_channel, _guild_channel_list, _guild_channel_board if bool(_guild_channel_list): elapsed_time = time() - timer_channel if elapsed_time < 86400 and not override: return FileLogger.info(f'Setting up channels in {guild.name}') _guild_channel_list = {} for channel in guild.channels: if channel.type.name == 'text' and channel.category.name.endswith( '公會戰討論區'): if channel.name.startswith('一王'): _guild_channel_list[1] = channel.id _guild_channel_list[channel.id] = 1 elif channel.name.startswith('二王'): _guild_channel_list[2] = channel.id _guild_channel_list[channel.id] = 2 elif channel.name.startswith('三王'): _guild_channel_list[3] = channel.id _guild_channel_list[channel.id] = 3 elif channel.name.startswith('四王'): _guild_channel_list[4] = channel.id _guild_channel_list[channel.id] = 4 elif channel.name.startswith('五王'): _guild_channel_list[5] = channel.id _guild_channel_list[channel.id] = 5 if channel.type.name == 'text' and channel.name.endswith('刀傷登記區'): _guild_channel_board = channel.id timer_channel = time()
def clear_line(boss_id: int) -> bool: global _guild_lines if boss_id in _guild_lines: _guild_lines[boss_id]["player_ids"] = {} elif boss_id == 0: for key in _guild_lines: _guild_lines[key]["player_ids"] = {} else: return False backup() FileLogger.info('clear_line executed') return True
async def on_message(message): if message.content == '!stop' and message.author.guild_permissions.administrator: FileLogger.info('User requested shutdown') execute() await client.logout() return # we do not want the bot to reply to itself if message.author.bot: return if message.content.startswith('!') or message.content.startswith('!'): setup_guild_channel_list(message.author.guild) setup_guild_member_list(message.author.guild) user_auth = { 'guild_id': message.author.guild.id, 'user_id': message.author.id, 'user_admin': message.author.guild_permissions.administrator, 'channel_id': message.channel.id } content = message.content[1:] if message.attachments: content += f' {message.attachments[0].url}' msg = parse_args(user_auth, content) if msg: if isinstance(msg, Mapping): # it's a dict embed = get_embed(message.author, msg) await message.channel.send(embed=embed) elif isinstance(msg, list): # it's a list embed = embed_template(message.author) for i in range(len(msg)): embed.add_field(name=i, value=msg[i], inline=False) await message.channel.send(embed=embed) elif is_url(msg): # it's an url embed = Embed(color=0xffa200) embed.set_image(url=msg) await message.channel.send(embed=embed) else: await message.channel.send(msg)
def parse_args(user_auth, string): args = strQ2B(string).split() response = '' if not args: return response # Find command, otherwise consider it as spam cmd = get_cmd(args[0]) if cmd: args = args[1:] else: cmd = "spam" # Create the instance try: inst = getattr(globals()[cmd], cmd)() except KeyError: FileLogger.warn(f'No command found') except Exception: FileLogger.exception(f'Exception at {__file__} {__name__}') # Execute the function FileLogger.info(f"{user_auth['user_id']} call {cmd} with {args}") try: if len(args) == 1 and args[0] == 'help': response = inst.usage elif not inst.check_param(args): response = inst.usage elif not inst.check_auth(user_auth): response = inst.auth_warning else: response = inst.run(user_auth, args) except Exception: FileLogger.exception(f'Exception at {__file__} {__name__}') return response
async def on_ready(): FileLogger.info(f'Logged in as {client.user.name}({client.user.id})') for guild in client.guilds: setup_guild_channel_list(guild, True) setup_guild_member_list(guild, True)
def get_settlement_time_object(): return datetime.now(_eu_moscow) def get_settlement_time(): return datetime.now(_eu_moscow).strftime('%Y-%m-%d') _transition_point = get_settlement_time_object().replace(hour=0, minute=0, second=0, microsecond=0) _local_transition_point = _transition_point.astimezone() FileLogger.info(f"Schedule daily task clear_line at {_local_transition_point}") schedule.every().day.at(_local_transition_point.strftime('%H:%M:%S')).do( clear_line, boss_id=0) cease_continuous_run = threading.Event() class ScheduleThread(threading.Thread): @classmethod def run(cls): while not cease_continuous_run.is_set(): schedule.run_pending() time.sleep(1) continuous_thread = ScheduleThread()