【问题标题】:How to make bot reply to a mention discord.py如何让机器人回复提及 discord.py
【发布时间】:2021-06-05 08:44:05
【问题描述】:

我正在制作一个不和谐的机器人,我遇到的一个问题是机器人不会回复用户,这是代码

    #Code Imports
import discord
import random
from discord import message
from discord import channel
from discord.embeds import Embed
from discord.ext import commands
from discord.ext.commands.bot import Bot
from discord.message import Message
from apikeys import *
from discord import guild
from discord import emoji
from discord import *
from discord import ContentFilter
from discord import channel
from discord import client

#intents
intents=discord.Intents.default()
intents.members=True
#prefix
client=commands.Bot(command_prefix='!',intents=intents)
#Start
@client.event
async def on_ready():
    for emoji in client.emojis:
        print("Name:", emoji.name + ",", "ID:", emoji.id)
    print('Bot is Online {0.user}'.format(client))
    print("--------------------------------------------------------------------------------------------------------------------------------------------------------")
client.remove_command('help')
#Commands

@client.command()
async def work(ctx):
    await ctx.send('use !war to get up the war menu')

emojigood = '\N{THUMBS UP SIGN}'
emojibad="\N{THUMBS DOWN SIGN}"



@client.command()
async def help(ctx):
    embed=discord.Embed(title='Help', description='!war is currently under testing please do not complain about it', color=0x00000)
    await ctx.send(embed=embed) 

@client.command()
async def war(ctx): 
    embed = discord.Embed(title='War', description='You are starting a war, do you want to continue?', color=0x00000)
    msg = await ctx.send(embed=embed)
    await msg.add_reaction(emojigood)
    await msg.add_reaction(emojibad)
    def check(r, user):
        return (r.emoji == emojigood or r.emoji == emojibad) and r.message == msg
    #Checks whether the message is the same, and the emoji is one of the accepted ones, and returns either True or False
    r, user = await client.wait_for('reaction_add',timeout=10 ,check=check)
    #this is equivalent to a event listener within your command, it will stop there until a reaction that meets the requirements has been found 
    #(This won't block other commands and functions)
    if r.emoji == emojigood:
        embed = discord.Embed(title='War', description='Please now choose a country', color=0x00000)
        await ctx.send(embed=embed)


prefix = "!" #insert your prefix 
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == f'{prefix}war': #dont need to use this string, just illustrating the point
        await message.channel.send("What country do you want to start a war with?")
        def check(msg):
            return msg.author == message.author and len(msg.role_mentions) > 0
        msg = await client.wait_for('message', check=check)
        role = msg.role_mentions[0]
        channel = client.get_channel(849230881994047508)
        await channel.send(f"{role.mention} {message.author} has declared war on you.")

    
    
     


    

  



client.run(token)

会发生什么: Bot:你想和哪个角色开战 用户:@something 机器人:向联合国发送信息,“@something 上的战争”

我不知道如何让它看到角色提及

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您有几个问题,使您正在使用的代码正常运行的方法是将message.content 替换为message.clean_content,但是,1. 这将对任何以@ 或提及开头的消息作出反应,这可能不是您想要的,并且 2. 任何不是开始提及的消息都不起作用。

    您在描述中说您正在寻找角色提及,这是一个不同的属性,并且您正在寻找发送消息并等待响应,所以这里有一段示例代码可以解决这些问题问题,假设我正确解释了您的问题:

    def check(msg):
        return msg.author == message.author and len(msg.role_mentions) > 0
    msg = await client.wait_for('message', check=check)
    role = msg.role_mentions[0]
    channel = client.get_channel(ID of the channel)
    await channel.send(f"{role.mention} {message.author} has declared war on you."
    

    如果我误解了您想要的任何内容,或者您​​不确定其中的某些内容是如何工作的,请给我留言,我会尽力解释!

    使用的稍微复杂的对象的文档参考: role_mentions, wait_for(),

    【讨论】:

    • 我其实不需要 if message.content == f'{prefix}war start': #dont需要使用这个字符串,只是说明点await message.channel.send("你想和哪个国家开战?我的终端里已经有那个代码了,我需要删除python中的代码吗?
    • 如果你没有它,它会响应每一条不是自己发送的消息,这会非常烦人
    • 既然我不知道如何解决这个问题,我把我的代码发给你,
    • 现在应该应用得更好
    • 现在我得到一个错误,说 await 需要异步,当我输入异步时它显示相同的错误,删除返回代码有帮助
    猜你喜欢
    • 1970-01-01
    • 2020-11-07
    • 2021-01-27
    • 2021-09-03
    • 2018-08-27
    • 2020-09-01
    • 2021-10-22
    • 2019-04-16
    • 2021-08-03
    相关资源
    最近更新 更多