栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

反应分页按钮前进和后退python

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

反应分页按钮前进和后退python

这是我想出的。

基本上,我们有一个循环,可根据所需反应检查每个反应,然后删除旧消息并发送新消息(如果我们看到了所寻找的反应之一)。

from discord.ext import commandsbot = commands.Bot(command_prefix='!')left = '⏪'right = '⏩'messages = ("1", "2", "3")def predicate(message, l, r):    def check(reaction, user):        if reaction.message.id != message.id or user == bot.user: return False        if l and reaction.emoji == left: return True        if r and reaction.emoji == right: return True        return False    return check@bot.command(pass_context=True)async def series(ctx):    index = 0    while True:        msg = await bot.say(messages[index])        l = index != 0        r = index != len(messages) - 1        if l: await bot.add_reaction(msg, left)         if r: await bot.add_reaction(msg, right)        # bot.wait_for_reaction        react, user = await bot.wait_for_reaction(check=predicate(msg, l, r))        if react.emoji == left: index -= 1        elif react.emoji == right: index += 1        await bot.delete_message(msg)bot.run("TOKEN")

有人要求提供一种用于编辑消息的版本,而不是发送新消息,我也将其更新为最新版本:

@bot.command(pass_context=True)async def series(ctx):    index = 0    msg = None    action = ctx.send    while True:        res = await action(content=messages[index])        if res is not None: msg = res        l = index != 0        r = index != len(messages) - 1        if l: await msg.add_reaction(left)         if r: await msg.add_reaction(right)        react, user = await bot.wait_for('reaction_add', check=predicate(msg, l, r))        if react.emoji == left: index -= 1        elif react.emoji == right: index += 1        action = msg.edit


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/651546.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号