send_message函数不适用于python discord bot

时间:2019-07-23 20:11:27

标签: python discord

我正在尝试使用send_message在遇到批评者时将消息发送到特定频道。我已经尝试了所有方法来做到这一点,我也使用了在该站点上找到的指南,因此我知道问题是相似的,但请耐心等待。我没有收到任何错误消息或崩溃,程序仅继续运行。我试图使用discord.Object(id ='')和client.get_channel(id)获取频道。

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')

1 个答案:

答案 0 :(得分:1)

您需要编写充当回调函数的协程,而不是编写立即执行的代码,然后该bot可以在某些情况下运行。在下面,我使用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")

关键是您的机器人总是侦听来自服务器的事件,并通过运行您定义的代码来对它们做出反应。您可以在聊天中使用>:start来运行上述命令。

send_messagesend方法的旧版本,已被弃用。确保您使用的是最新版本的discord.py,并使用send代替send_message