当满足条件时,我尝试使用 send_message 将消息发送到特定 channel 。我已经尝试了各种方法来做到这一点,我也使用了在这个网站上找到的指南,所以我知道问题是相似的,但请耐心等待。我没有收到任何错误消息或崩溃,程序只是继续运行。我尝试使用discord.Object(id='') 和client.get_channel(id) 获取 channel 。
import discord
import asyncio
from discord.ext import commands
client = discord.Client()
command = input('>: ')
if command == 'start':
channel = client.get_channel("server-status")
client.send_message(channel, "Server is up!")
client.run('token')
最佳答案
您需要编写充当回调函数的协程,而不是编写立即执行的代码,然后机器人可以在某些情况下运行这些协程。下面,我使用 Bot.command 装饰器告诉机器人在看到与前缀一起使用的协程名称时将协程注册为回调。
from discord.ext.commands import Bot
bot = Bot(">:")
@bot.command()
async def start(ctx):
await ctx.send("Server is up!")
bot.run("token")
关键是您的机器人始终监听来自服务器的事件,并通过运行您定义的代码对它们使用react。您可以在聊天中使用 >:start
运行上述命令。
send_message
是 send
方法的旧版本,已被弃用。确保您使用的是最新版本的discord.py并使用send
而不是send_message
关于python - send_message 函数不适用于 python discord bot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57171517/