python - Discord.py - 当有人提到他时,机器人不回答
问题描述
当有人在频道中提到机器人时,我尝试这样做,机器人会向用户发送一条随机消息,但我尝试过的所有事情都没有奏效。(discord.py)
channel = 797224597443051611
@bot.event
async def on_message(msg):
chat = bot.get_channel(797224597443051611)
if msg.channel.id != channel:
return
if bot.user.mentioned_in(msg):
await chat.send(msg.author.mention + " " + random.choice(text))
await bot.process_commands(msg)
我也尝试过这种方法,但它也不起作用。
channel = 797224597443051611
@bot.event
async def on_message(msg):
if msg.channel.id != channel:
return
if bot.user.mentioned_in(msg):
await msg.send(msg.author.mention + " " + random.choice(text))
await bot.process_commands(msg)
解决方案
这是我对你的问题的回答。我已经包含更多代码以获得更多上下文。
import discord
from discord.ext import commands
client = discord.Client()
client = commands.Bot(command_prefix='!')
@client.event
async def on_message(message):
if message.author == client.user:
return
mention = f'<@!{client.user.id}>'
if str(message.content).startswith(mention):
await message.channel.send('Your message... Testing')
这应该是您正在寻找的。如果您想要随机响应,只需将发送消息更改为列表中的随机项目或文本文档中的随机行。
如果需要,我将编辑答案以包含列表中的随机响应。
推荐阅读
- java - java 类检测,在任何 jdbc 连接实例创建之前注入代码
- flutter - 如何将异步函数中声明的变量调用到颤动的小部件中?
- arrays - 是否有与 ( (ptr + i)->marks ) 等价的形式,例如 ( ptr[i] .... )?
- flutter - struct.pack(' 的等效函数
在 Python 和 JavaScript 中都有一个名为
pack
在 JavaScript 中:
struct.pack('<I', 5311)
在 Python 中
pack("<I", 5311)
<- mongodb - 在匹配阶段后将展开的数组分组并保留父数据/结构
- python - Keras 多次预测
- office365 - 在 Outlook 加载项中,我可以在不使用图形 Api 包的情况下获取办公室图形 API 访问令牌吗
- python - 无法弄清楚如何在海龟模块中使用动画使子弹移动
- javascript - 根据条件减少 ngFor 循环变量
- reactjs - 在变量中遍历jsx,然后通过附加到ref来渲染它