Python-discord.py on_message-传递位置参数



我试图使用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服务器上通道的名称,文本数据-文本文件的文件路径,命令-用户将在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")

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

提前感谢任何能够提供指导或解决方案的人。

当机器人通过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)

最新更新