【问题标题】:getting messages from discord to append to list with python使用python从discord获取消息以附加到列表
【发布时间】:2021-05-22 14:07:08
【问题描述】:

我正在尝试从 discord 中获取消息并将消息放入名为 strlst 的列表中,但到目前为止,它并未将消息放入 strlst 并且终端中没有错误。因为我同时使用了@bot.event 和@bot.command(),所以我放了一个进程命令(idk 那是什么)。这里是:

import discord 
from discord.ext import commands 
@bot.event
async def on_message(ctx):
    await bot.process_commands(ctx)

    if ctx.content == ".add ":
        ctx = ctx.content.lower()
        newctx = ctx[4:]
        strlist.append(newctx)

strlist = ["!cb","hello","no thank you", "you pass the butter"]

@bot.command(name="bb")
async def bb(ctx):
    await ctx.send(f"WHAT DO YOU WANT ?")

    # This will make sure that the response will only 
be registered if the following
    # conditions are met:
    def check(msg):
        return msg.author == ctx.author and 
msg.channel == ctx.channel and \
        msg.content.lower() in strlist

    msg = await bot.wait_for("message", check=check)
    if msg.content == '!cb':
        await ctx.send("GOING DOWN . . .")
    else:
        await ctx.send("OK , COOL STORY . . .")
        



    @bot.command(name="lst", help = "List of responses it will reply to", brief = f"The current responses available are:\n {strlist}\nNOTE:!cb cancels the bot")
    async def current_lst(msg):
        await msg.send("To add to the list, simply type .add [your message]")

这是有效的部分:

用户:!bb

机器人:你想要什么?

用户:你放弃了

Bot:好的,很酷的故事。 . .

用户:!bb

机器人:你想要什么?

用户:!cb

机器人:正在下降。 . .

我希望 .add 的部分像这样工作:

用户:.add 我有一个很酷的故事(机器人将它添加到 strlst)

用户:!bb

机器人:你想要什么?

用户:我有一个很酷的故事

机器人:很酷的故事兄弟。 . .

要点:我想将用户的消息添加到列表中。如果有其他方法可以做到这一点,我们将不胜感激。

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    布尔值if ctx.content == ".add ": 只检查消息是否完全等于“.add”。使用ctx.content.startswith('.add')。您还可以使用print 或调试器来识别程序中的值以及问题可能出在哪里。

    至于可读性,你不应该这样做ctx = ctx.content.lower(),因为这会用字符串覆盖消息对象,这只会导致混乱

    【讨论】:

    • 我试过了,它仍然没有将 .add 之后的消息添加到列表中,但是使用 print 我想我知道该怎么做,谢谢!
    猜你喜欢
    • 2020-07-27
    • 2020-09-24
    • 1970-01-01
    • 2020-11-10
    • 2021-07-19
    • 2020-12-23
    • 2020-12-07
    • 2021-03-04
    • 2021-07-28
    相关资源
    最近更新 更多