Esempio n. 1
0
def text_broadcast(bot, update):
    if is_admin(update.message.from_user.id):
        msg = update.message.text.replace('/tbcast ', '', 1)
        if msg == '':
            update.message.reply_text('Please, write text!')
        else:
            for uid in db_user.get():
                try:
                    bot.sendMessage(chat_id=uid, text=msg)
                except Unauthorized:
                    pass
Esempio n. 2
0
    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))
Esempio n. 3
0
def schedule_broadcast_teacher(bot):
    for uid in db_user.get():
        name = db_user.get_sub_teacher(uid)
        if name is not None:
            try:
                bot.sendMessage(chat_id=uid, text=schedule.get_teacher(name=name))
            except schedule.ScheduleException as ex:
                if ex.code == 201:
                    bot.sendMessage(chat_id=uid, text='У преподавателя {} нет пар'.format(name))
                else:
                    bot.sendMessage(chat_id=uid, text='Ошибка {}: {}'.format(ex.code, ex.message))
            except Unauthorized:
                pass
Esempio n. 4
0
def schedule_broadcast_student(bot):
    for uid in db_user.get():
        group = db_user.get_sub_student(uid)
        if group is not None:
            try:
                bot.sendMessage(chat_id=uid, text=schedule.get_student(group=group))
            except schedule.ScheduleException as ex:
                if ex.code == 201:
                    bot.sendMessage(chat_id=uid, text='У группы {} нет пар'.format(group))
                else:
                    bot.sendMessage(chat_id=uid, text='Ошибка {}: {}'.format(ex.code, ex.message))
            except Unauthorized:
                pass
Esempio n. 5
0
 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()
Esempio n. 6
0
    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()
Esempio n. 7
0
 async def point(self, ctx):
     """查詢自己的跑團點數"""
     user = db_user.get(ctx.author.id)
     response = "你目前的點數為:{}".format(user.point)
     await ctx.send(response)
Esempio n. 8
0
def get_info_by_token(access_token):
    info = db_session.get(access_token)
    info['permissions'] = db_user.get(info['username'])['permissions']
    return info