def processor(bot, update): user = update.message.from_user text = update.message.text action, params = db_user.get_action(user.id) settings = utils.get_settings() db_user.update(user) # Check if it is group chat if update.message.chat.id < 0: if text.startswith('/'): log_message( update.message.reply_text(utils.get_constant('not_chat'))) return # logging logger.info('{} {} ({}:@{}): {}'.format(user.first_name, user.last_name, user.id, user.username, text)) # Constant behavior for button in settings['constant_behavior']: if utils.get_constant(button) == text: behave(settings['constant_behavior'][button], bot, update) return # Checking menu if action in settings: for button in settings[action]['keyboard']: if get_button_text(button) == text: behave(button, bot, update) else: action_manager(bot, update, action)
async def edit(self, ctx, *args): """(管理者功能)修改使用者資料 範例: edit @DolaTRPG point 10 """ target_parsed = re.findall('^<@!?(\d+)>$', args[0]) if not target_parsed: await ctx.send("目標不存在,請在伺服器內執行此指令") return target_discord_user = self.bot.get_user(int(target_parsed[0])) update_item = {args[1]: int(args[2])} db_user.update(target_discord_user.id, **update_item)
async def on_message(self, message): """user takes action in discord server Args: (Discord.User) discord user class """ discord_user = message.author user = db_user.get(discord_user.id) if not user: # add user if not exist db_user.add(id=discord_user.id, name=discord_user.name) return if user.activity_at < datetime.utcnow() - timedelta(hours=12): # user can get points for every 12 hours db_user.update(user.id, activity_at=datetime.utcnow(), point=user.point + 1) await self.ban_abandoned_users()
async def donate(self, ctx, *args): """轉讓點數給其他人 範例: give @DolaTRPG 10 give @DolaTRPG 10 轉讓理由""" user = db_user.get(ctx.author.id) target_parsed = re.findall('^<@!?(\d+)>$', args[0]) if not target_parsed: await ctx.send("目標不存在,請在伺服器內執行此指令") return target_discord_user = self.bot.get_user(int(target_parsed[0])) target_user = db_user.get(target_discord_user.id) donate_point = int(args[1]) if user.point < donate_point: # not enough points await ctx.send("你持有的點數({})不夠轉讓({})".format(user.point, donate_point)) return # points transition db_user.update(user.id, point=user.point - donate_point) db_user.update(target_user.id, point=target_user.point + donate_point) # notify users await ctx.author.send("已轉讓 {} 點給 {},你的剩餘點數為 {}".format( donate_point, target_discord_user.mention, user.point - donate_point)) await target_discord_user.send("{} 轉讓 {} 點給你,你的現有點數為 {}".format( ctx.author.mention, donate_point, target_user.point + donate_point)) comment = " ".join(args[2:]) if comment: await target_discord_user.send("轉讓理由:{}".format(comment)) await util.log( self.bot, "轉讓點數:{}->{}, 點數:{}".format(ctx.author.mention, target_discord_user.mention, donate_point))
async def start(self, message): if message.edited_at is not None: # take no action for edited message return gm = message.mentions[0] start_flag = False # collect player list players = [] for reaction in message.reactions: async for user in reaction.users(): if reaction.emoji == '🆙': players.append(user) elif reaction.emoji in ['1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣']: if user.id != gm.id: # ignore non-gm reactions continue start_flag = True if reaction.emoji == '1⃣': player_count = 1 elif reaction.emoji == '2⃣': player_count = 2 elif reaction.emoji == '3⃣': player_count = 3 elif reaction.emoji == '4⃣': player_count = 4 elif reaction.emoji == '5⃣': player_count = 5 elif reaction.emoji == '6⃣': player_count = 6 players.remove(self.bot.user) # remove bot user if start_flag: await util.log(self.bot, "正在開團({})".format(message.id)) # filter player by gm setting game_point = int( parse_message(message.content, "開團時酌收 (\d+) 點跑團點數")) game_title = parse_message(message.content, " 要開團囉\((.*)\)!!\n") await util.log(self.bot, "團名:{}, 點數:{}".format(game_title, game_point)) await util.log( self.bot, "團名:{}, 報名者:{}".format(game_title, [p.mention for p in players])) # valid player by points valid_players = db_user.get_game_players( ids=[p.id for p in players], required_points=game_point, player_count=player_count) valid_player_ids = [p.id for p in valid_players] players = [p for p in players if p.id in valid_player_ids] # verify number of players await util.log( self.bot, "團名:{}, 目標人數:{}, 成團玩家:{}".format(game_title, player_count, [p.mention for p in players])) if len(players) < player_count: await message.channel.send("{} 的 {} 因為人數不足而流團".format( gm.mention, game_title)) await message.edit(content=message.content + "\n(流團)") return # start successfully player_mentions = [du.mention for du in players] await message.channel.send("{} 的 {} 已收團\n玩家:{}".format( gm.mention, game_title, " ".join(player_mentions))) # send dm to players for du in players: user = db_user.get(du.id) db_user.update(du.id, point=user.point - game_point, use=user.use + game_point) await send_direct_message( du, "恭喜入選 {} 的 {} ".format(gm.name, game_title)) await send_direct_message( du, "使用點數 {},剩餘點數:{}".format(game_point, user.point - game_point)) # send dm to gm user = db_user.get(gm.id) total_point = int(game_point) * len(players) db_user.update(gm.id, point=user.point + total_point, earn=user.earn + total_point) await send_direct_message( gm, "開團成功,玩家:{}".format(",".join([p.name for p in players]))) await send_direct_message( gm, "獲得點數 {},合計點數:{}".format(total_point, user.point + total_point)) # edit original message await message.edit(content=message.content + "\n(已收團)") users.write()