【问题标题】:Python - discord.py on_message - pass positional argumentsPython - discord.py on_message - 传递位置参数
【发布时间】:2020-07-14 10:16:40
【问题描述】:

我正在尝试使用 discord.py 将位置参数包含到 on_message 函数中,但我收到以下错误

 File "/usr/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() missing 3 required positional arguments: 'channels', 'textdata', and 'command'

我的目标是传递频道 - discord 服务器上的频道名称,textdata - 文本文件的文件路径,command - 用户将在 discord 频道中输入以返回数据的命令。

我只是想问一下是否可以将位置参数传递给 on_message 或者 message 可以是唯一的吗?

我的代码是

channels =[]

@client.event
async def on_message(message, channels , textdata, command):
    
    id = client.get_guild(7319460*****229982)
    
    
    
    
    if str(message.channel) in channels:
        if message.content.find(command) != -1:

            with open(textdata, 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()
                    
def callall():
    on_message(channels="boxingmma",  textdata="/home/brendan/Desktop/Python/liveonsatscraper/testlasttime.txt", command="!boxingfixtures")

我尝试这样做的原因是因为我需要能够在多个不和谐通道上运行上面的代码,从 textdata 中的文件路径输出数据,并根据所需的数据提供不同的命令。我可以将代码复制 100 次以上并为每个参数输入硬编码的字符串值,但我认为最好将这些值传递给上面的函数以保存重复代码。

提前感谢任何可以为此提供指导或解决方案的人。

【问题讨论】:

    标签: python text-files discord.py


    【解决方案1】:

    当bot通过on_message接收消息时,只有一个参数:message。 这是一个单独的类,但它可以分解为:

    message.content #what was said, string
    message.channel #which channel it was said in, channel class
    message.guild #which server it was said in, guild class
    message.author #who sent the message, user class
    

    你可以从那里开始。

    {编辑}

    如果您希望它也能够解析命令,请这样做:

    @bot.event
    async def on_message(message):
        if message.author == bot.user: return #makes it so the bot doesn't listen to itself
        await bot.process_commands(message)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2019-08-23
      • 2022-09-23
      • 1970-01-01
      • 2018-11-15
      相关资源
      最近更新 更多