简体   繁体   中英

send_message function not working for python discord bot

I am trying to use send_message to send a message to a specific channel when a critieria is met. I have tried every way to do this, i have used guides i found on this site as well so I know the question is similar but please bear with. I get no error message or crash the program just continues to run. I have tried to get the channel using discord.Object(id='') and 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')

Instead of writing code that executes immediately, you need to write coroutines that function as callbacks, which the bot can then run under certain circumstances. Below, I use the Bot.command decorator to tell the bot to register the coroutine as a callback when it sees the name of the coroutine used with the prefix.

from discord.ext.commands import Bot

bot = Bot(">:")

@bot.command()
async def start(ctx):
    await ctx.send("Server is up!")

bot.run("token")

The key is that your bot is always listening for events from the server and reacts to them by running code that you've defined. You would run the above commands with >:start in chat.

send_message is the old version of the send method, and has been deprecated. Make sure you're using the latest version of discord.py and use send instead of send_message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM