Reputation: 43
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')
Upvotes: 1
Views: 520
Reputation: 60944
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
Upvotes: 1