如何创建一个简单的机器人来自动响应包含某个触发语句的消息?

2022-01-15 00:00:00 python discord.py

问题描述

我正在为我和我的朋友设置一个 Discord 服务器,我需要一个能够对输入了某个触发短语的任何消息添加 20 条反应的机器人.我曾经有一个机器人可以做到这一点,但它已经更新并限制为 3 个反应,这对我不起作用.

I'm setting up a Discord server for me and my friends, and I'm in need of a bot able to add 20 reactions to any message in which a certain trigger phrase was typed. I used to have a bot that could do that, but it has been updated and limited to 3 reactions, which isn't working for me.

所以我在这里,以我对编程的非常基本的了解,试图为这个唯一目的设置一个非常基本的机器人.

So here I am, with my very basic understanding of programming, trying to setup a very basic bot for that sole purpose.

我已经尝试过在 YT 上找到的一个简单的机器人提议,但它根本不起作用,我不知道为什么以及如何解决它.

I already tried a simple bot proposition found on YT, but it didn't work at all, and I'm at a loss to know why and how to fix it.

所以这是我尝试过但没有成功的提议:

So this was the proposition I tried and didn't manage to make functional:

import discord
from discord.ext    import commands
from discord.ext.commands   import Bot
import asyncio

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    print ("Ready to react, boss !")


@bot.event
async def on_message(message):
    if(message.channel.id == "550373218758688790"):
        await bot.add_reaction(message, ":war_tank:552569109108490252")


bot.run("NTY5OTQ0NTMyMzIyNjE1MzI2.XL4IBg.WH-Ms1DWKJN8qGBBLAxdGye0q2I")

所以这个应该对每条消息做出反应,我打算从那里开始工作,但它甚至没有用.相反,我在每条消息的 cmd 日志中都收到一条消息,看起来像这样:

So this one was supposed to react to every message with a reaction, and I was planning on working from there, but it didn't even work. Instead, I got a message in the cmd log for each message and that looked like this:

Ignoring exception in on_message
Traceback (most recent call last):
  File "G:JeuxDiscord Botlibsite-packagesdiscordclient.py", line 255, in _run_event
    await coro(*args, **kwargs)
  File "G:BureauTotemReact Botot.py", line 16, in on_message
    await bot.add_reaction(message, ":war_tank:552569109108490252")
AttributeError: 'Bot' object has no attribute 'add_reaction'

因此,如果任何有实际技能的人(考虑到我的技能,我会很容易留下深刻印象)能给我指明正确的方向,我会非常高兴.

So if anyone with actual skills (considering mine, I will be easily impressed) can point me in the right direction, I will be more than happy.

感谢您的时间和关注.


解决方案

您正在查看旧教程.Client.add_reaction 已移至 <代码>Message.add_reaction 在 discord.py 1.0

You're looking at old tutorials. Client.add_reaction was moved to Message.add_reaction in discord.py 1.0

您描述的功能可能类似于:

The functionality you describe could look something like:

default_emojis = [
    "N{GRINNING FACE}",
    "N{KEYCAP DIGIT ONE}"
]

custom_emojis = [
    "war_tank"
]

async def react(message):
    for emoji in default_emojis:
        await message.add_reaction(emoji)
    for emoji in message.guild.emojis:
        if emoji.name in custom_emojis:
            await message.add_reaction(emoji)

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if "react to me" in message.content.lower():
        await react(message)

相关文章