示例#1
0
async def tagrc(session: CommandSession):
    # 从会话状态(session.state)中获取城市名称(city),如果当前不存在,则询问用户
    # tags = session.get('tags', prompt='输入tag列表,空格隔开')
    # 获取城市的天气预报
    setu_path = await get_setu_path()
    if not setu_path: return
    msg = Message("test")
    msg_seg = MessageSegment.image(setu_path)
    msg.append(msg_seg)
    print(msg)
    print(str(msg))
    print(str(msg_seg))
    # 向用户发送天气预报
    await session.send(msg)
示例#2
0
文件: vote.py 项目: Chuibility/cgbot
async def vote(s: CommandSession, user, group, user_in_group, state):
    parser = ArgumentParser(session=s, usage=USAGE)
    parser.add_argument(
        'command',
        choices=['list', 'new', 'yea', 'nay', 'commit', 'count', 'announce'])
    parser.add_argument('ITEM')
    parser.add_argument('extra', nargs='?')
    args = parser.parse_args(s.argv)

    name = args.ITEM.strip()
    db: orm.Session = orm.getSession()
    vote_item = db.query(VoteItem).filter(VoteItem.GroupId == group.GroupId,
                                          VoteItem.Name == name).one_or_none()
    try:
        if args.command == 'list':
            msg = Message("List of available vote items:\n")
            items = None
            if args.extra == 'all':
                # Extract all items
                items = db.query(VoteItem).filter(
                    VoteItem.GroupId == group.GroupId).all()
            else:
                # Extract active items
                items = db.query(VoteItem).filter(
                    VoteItem.GroupId == group.GroupId,
                    VoteItem.Committed == False).all()
            for item in items:
                msg.append(
                    MessageSegment.text(
                        f"'{item.Name}' - {item.Description}\n"))
            await s.send(msg)

        elif args.command == 'new':
            if not vote_item:
                if not args.extra:
                    desc = f"Vote on {name}"
                else:
                    desc = args.extra
                item = VoteItem(GroupId=group.GroupId,
                                ProposerId=user.UserId,
                                Name=name,
                                Committed=False,
                                Description=desc)
                db.add(item)
                db.commit()
                await s.send(
                    f"Vote item '{item.Description}' ({item.Name}) created.",
                    at_sender=True)
            else:
                await s.send(f"Vote item '{vote_item.Name}' already exisist.",
                             at_sender=True)
        elif args.command == 'yea':
            if not vote_item:
                raise _VoteItemNotFound()
            if vote_item.Committed:
                raise _VoteClosed()
            vote = db.query(Vote).filter(
                Vote.Item == vote_item.Id,
                Vote.Voter == user.UserId).one_or_none()
            if not vote:
                vote = Vote(Item=vote_item.Id,
                            Voter=user.UserId,
                            Ballot=_BALLOT_YEA)
                db.add(vote)
            else:
                vote.Ballot = _BALLOT_YEA
            db.commit()
            await s.send(
                f"You voted YES to '{vote_item.Description}' ({vote_item.Name}).",
                at_sender=True)

        elif args.command == 'nay':
            if not vote_item:
                raise _VoteItemNotFound
            if vote_item.Committed:
                raise _VoteClosed
            vote = db.query(Vote).filter(
                Vote.Item == vote_item.Id,
                Vote.Voter == user.UserId).one_or_none()
            if not vote:
                vote = Vote(Item=vote_item.Id,
                            Voter=user.UserId,
                            Ballot=_BALLOT_NAY)
                db.add(vote)
            else:
                vote.Ballot = _BALLOT_NAY
            db.commit()
            await s.send(
                f"You voted NO to '{vote_item.Description}' ({vote_item.Name}).",
                at_sender=True)

        elif args.command == 'commit':
            if not vote_item:
                raise _VoteItemNotFound()
            if vote_item.ProposerId != user.UserId:
                raise _NotVoteOwner()
            vote_item.Committed = True
            db.commit()
            await s.send(
                f"You concluded vote item '{vote_item.Description}' ({vote_item.Name}).",
                at_sender=True)

        elif args.command == 'count':
            if not vote_item:
                raise _VoteItemNotFound
            total_count = db.query(Vote).filter(
                Vote.Item == vote_item.Id).count()
            positives = db.query(Vote).filter(
                Vote.Item == vote_item.Id, Vote.Ballot == _BALLOT_YEA).count()
            await s.send(
                f"Vote item '{vote_item.Description}' ({vote_item.Name}) got {positives} YES among {total_count} votes."
            )

        elif args.command == 'announce':
            if not vote_item:
                raise _VoteItemNotFound
            total_count = db.query(Vote).filter(
                Vote.Item == vote_item.Id).count()
            # proposer = db.query(orm.User).filter(orm.User.UserId==vote_item.ProposerId).one()
            positives = db.query(Vote).filter(
                Vote.Item == vote_item.Id, Vote.Ballot == _BALLOT_YEA).all()
            # positives = db.query(Vote, orm.User) \
            #               .join(Vote.Voter == orm.User.UserId) \
            #               .filter(Item=vote_item.Id, Ballot=_BALLOT_YEA) \
            #               .all()
            msg = nonebot.Message(
                f"Vote item '{vote_item.Description}' ({vote_item.Name}) initiated by "
            )
            msg.append(MessageSegment.at(vote_item.ProposerId))
            msg.append(
                MessageSegment.text(
                    f" got {len(positives)} YES among {total_count} votes. "))
            msg.append(
                MessageSegment.text(
                    f"The following users in favor of the item:"))
            segs = [
                nonebot.MessageSegment.at(vote.Voter) for vote in positives
            ]
            for seg in segs:
                msg.append(seg)
            await s.send(msg)
        else:
            raise Exception

    except _VoteItemNotFound:
        await s.send(f"Vote item '{name}' does not exist.", at_sender=True)

    except _NotVoteOwner:
        await s.send(f"You does not own vote item '{name}'.", at_sender=True)

    except _VoteClosed:
        await s.send(f"Voting for '{name}' is already closed.", at_sender=True)