Im trying to make a discord bot, and I want its first command to be pretty simple, I just want it to respond "pong!" when someone types "!ping" but when I try to run the command, nothing happens.. I've tried using other commands too but they all result in errors.. I'm a beginner to using Python, so please, can someone tell me what's wrong with my code, and how I can fix it?
In short, my bot doesn't respond back when I type the command I made for it to do so.
import discord
import os
from discord.ext import commands
from discord.ext.commands import Bot
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#bot.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
Instead of #bot.command you should use #client.command.
Also no need for the from discord.ext.commands import Bot import
So your new code would be
import discord
import os
from discord.ext import commands
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
#client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#client.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
You could just do inside on_message code block
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!ping'):
await message.channel.send('Pong!')
client.run('your token here')
Related
The command "!hungry" isn't getting responded to by the bot. If I remove the if statement on line 19, the bot will respond to any message, but once I add the if statement for the specified string, I don't get any response back.
I need help on how to approach this issue. I have the code and error it gave error:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'{client.user} is now connected!')
#client.event
async def on_message(message):
if message.author == client.user:
return
** if message.content == '!hungry':
await message.channel.send('Sure. Here you go.')
await client.process_commands(message)**
client.run(os.getenv('TOKEN'))
This code without the if statement runs fine as the bot responds back to anything I type as seen below:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'{client.user} is now connected!')
#client.event
async def on_message(message):
if message.author == client.user:
return
** await message.channel.send('Sure. Here you go.')
await client.process_commands(message)
**
client.run(os.getenv('TOKEN'))
You don't have the message_content intent, so you can't read messages. Refer to the docs for more info on intents.
Also, consider just using the built-in commands framework instead of manually parsing messages.
I had the same issue.
I could make it work by toggling 'Privileged Gateway Intents' on Discord developer's page. You can find in the Bot section of your bot.
I used the below code :
(Sorry for the format I am still new to the Stack Overflow)
import discord
intent = discord.Intents.all()
client = discord.Client(intents = intent)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
if message.author.name == client.user: return
if message.content.startswith('hello'):
await message.channel.send('Hello!')
client.run("token")
my discord bot isn't replying to commands does anyone know why? i also get an error that says discord.gateway: Shard ID None has connected to Gateway
token = 'mytoken'
import discord
client = discord.Client(intents=discord.Intents.default())
#client.event
async def on_ready():
print('we are logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client:
return
if message.content.startswith('$info'):
message.channel.send(f'> i am a bot currently being developed by agent')
client.run(token)
You should use a bot instead of a client, and don't forget to await async functions.
try this:
import discord
from discord.ext import commands
token = 'mytoken'
bot = commands.Bot(command_prefix='$')
#bot.event
async def on_ready():
print('Ready!')
#bot.command()
async def info(ctx):
await ctx.send('> i am a bot currently being developed by agent')
bot.run(token)
now try $info with the bot running.
Try to use discord.ext.commands instead of discord.client
token = 'mytoken'
import discord
from discord.ext import commands
client = commands.Bot(intents=discord.Intents.default())
#client.event
async def on_ready():
print('we are logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client:
return
if message.content.startswith('$info'):
message.channel.send(f'> i am a bot currently being developed by agent')
bot.process_commands(message)
client.run(token)
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('your token here')
As mentioned above, I believe the issues come with the second async function or below, I tried testing it but there was no hello back. Thank you.
You ideally shouldn't be using discord.Client() here but discord.ext.commands.Bot()
Re-write :
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix="$")
#bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
#bot.command()
async def hello(ctx):
await ctx.send("Hello!")
bot.run(os.getenv("DISCORD_TOKEN"))
Which would result in :
Also, I can't see any issues in the code you provided other than the second if statement being incorrectly indented but even if that were the case you'll get an error.
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="-")
#client.event
async def on_message(message):
if message.content.startswith("uno regeln"):
await message.channel.send('http://www.uno-kartenspiel.de/spielregeln/')
#client.command()
async def test(ctx):
await ctx.send("Ok")
client.run("TOKEN")
When I try to run this and use -test nothing happens
But why?
I looked in the docs but in my opinion everything is fine
As given by the link provided by #Lukasz Kwieciński,
you need to change it to:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="-")
#client.event
async def on_message(message):
if message.content.startswith("uno regeln"):
await message.channel.send('http://www.uno-kartenspiel.de/spielregeln/')
await bot.process_commands(message)
#client.command()
async def test(ctx):
await ctx.send("Ok")
client.run("TOKEN")
import discord
from discord.ext import commands
intents = discord.Intents.default()
client = commands.Bot(command_prefix="!", intents=intents)
client.remove_command('help')
#client.event
async def on_ready():
print("Bot Is Ready")
await client.change_presence(activity=discord.Game(name="TYPE THERE ANYONE"))
#client.command()
async def test(ctx):
await ctx.send("Ok")
client.run('YOUR TOKEN')
Try this
import discord
import random
from discord.ext import commands
client = commands.Bot(command_prefix = '!')
#client.event
async def on_message(message):
print('Message from {0.author}: {0.content}'.format(message))
#client.event
async def on_ready():
deneme=["Besim Tibuk Online!","Bu Kitap benim","Satacağız!","Özelleştireceğiz!"]
await client.get_channel(#########).send(random.choice(deneme))
#client.command()
async def ping(ctx):
await ctx.send("Pong")
"client.command()" side cant working.When ı write "ping" I cant get response
Its because of your on_message, you arent processing commands. To process commands, do await client.process_commands(message). Here is the reworked code.
#client.event
async def on_message(message):
print('Message from {0.author}: {0.content}'.format(message))
await client.process_commands(message)