我正在尝试为朋友制作一个机器人,以便当有人在特定频道中发布图像时,机器人会添加反应。到目前为止,这是我的代码:
@bot.event
async def on_message(client, channel, message):
channel = 830207256418058330
pic_ext = ['.jpg','.png','.jpeg']
if len(message.attachments) > 0: #Checks if there are attachments
for file in message.attachments:
for ext in pic_ext:
if file.filename.endswith(ext):
print(f"This message has an Image called: {file.filename}")
await message.channel.add_reaction(tu)
await message.channel.add_reaction(td)
await message.channel.add_reaction(fire)
await message.channel.add_reaction(bl)
这是我收到的错误:
TypeError: on_message() missing 2 required positional arguments: 'channel' and 'message'
答案 0 :(得分:1)
正如 Łukasz 在评论中所说,on_message
只接受 message
作为参数。这将解决您问题中的 TypeError
。
使用您的频道 ID,从您的变量 channel
,您可以检查发送消息的频道 message.channel
是否与此 ID 相同。如果是,则通过 message.add_reaction
将反应添加到消息中。
@bot.event
async def on_message(message):
channel = 830207256418058330
pic_ext = ['.jpg','.png','.jpeg']
if message.channel.id == channel: # if the channel message was sent in matches the given id
# then your other code here