Skip to content Skip to sidebar Skip to footer

How Do I Send A Message To A Specific Channel? Discord/python

How do I send a message to a specific channel? Why am I getting this Error ? My ChannelID is right Code: from discord.ext import commands client = commands.Bot(command_prefix='!'

Solution 1:

The reason for the error is because channel = client.get_channel() is called before the bot is connected, meaning it will always return None as it cannot see any channels (not connected).

Move this to inside your command function to have it get the channel object as the command is called.

Also note that since version 1.0, snowflakes are now int type instead of str type. This means you need to use client.get_channel(693503765059338280) instead of client.get_channel('693503765059338280').

from discord.ext import commands


client = commands.Bot(command_prefix='!')


@client.eventasyncdefon_ready():
    print('Bot wurde gestartet: ' + client.user.name)

@client.command()asyncdeftest(ctx,name_schuh,preis,festpreis):
    channel = client.get_channel(693503765059338280)
    await channel.send("Name:" + name_schuh +"\n Preis: " + preis +"\n Festpreis: " + festpreis)

client.run('token')

Solution 2:

client and channel are out of scope. You can use global keyword for a dirty hack:

from discord.ext import commands

client = commands.Bot(command_prefix='!')
channel = client.get_channel(693503765059338280)

@client.eventasyncdefon_ready():
    global client
    print('Bot wurde gestartet: ' + client.user.name)

#wts        @client.command()asyncdeftest(ctx,name_schuh,preis,festpreis):
    global client
    global channel
    await channel.send(discord.Object(id=693503765059338280),"Name:" + name_schuh +"\n Preis: " + preis +"\n Festpreis: " + festpreis)

But the better alternative would be a handler class that holds the instances.

Post a Comment for "How Do I Send A Message To A Specific Channel? Discord/python"