python - send_message 函数不适用于 python discord bot

标签 python discord

当满足条件时,我尝试使用 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_messagesend 方法的旧版本,已被弃用。确保您使用的是最新版本的discord.py并使用send而不是send_message

关于python - send_message 函数不适用于 python discord bot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57171517/

相关文章:

python - 在 Python 中处理字节和二进制数据

python - 寻找一个python函数来查找字符串中最长的连续重复子串

python - Py2exe 可执行文件显示黑色命令行窗口

python - 在消息上删除消息 Discord.py

python - 如何在 Discord.py 中获取消息的时间戳?

javascript - Discord.js 从 message.content 获取 Unicode 表情符号

python - 通过参数传递异步循环或使用默认异步循环

python - 在 python 中解码 json

node.js - Discord.js 嵌入消息多行值

python - 如何使用带空格的命令名称?