My discord bot is not responding to my commands - python

import discord
import os
client = discord.Client(intents=discord.Intents.default())
#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'):
channel = message.channel
await channel.send('Hello!')
client.run(os.getenv('TOKEN'))
I tried to make a discord bot using discord.py. The bot comes online and everything but does not respond to my messages. Can you tell me what is wrong?

You seem to have an indentation error:
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = message.channel
await channel.send('Hello!')
The last if-statement is never going to be executed. Instead, move it one indentation back such that:
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
channel = message.channel
await channel.send('Hello!')

Related

on_message disables all my command even with bot.process bot.process_commands

So I am making a discord bot and I am trying to make a feature if you say "hi" the bot will say "hi" back. But using on_message all my other commands get disabled.
#client.event
async def on_message(ctx, message):
if message.content == "hi":
await message.channel.send('hi')
await bot.process_commands(ctx)
I would appreciate it if someone could help me fix this error.
on_message takes a single argument message and does not take ctx parameter and you need to pass message in bot.process_commands()
So it should look like this :
#bot.event
async def on_message(message):
if message.content == "hi":
await message.channel.send('hi')
await bot.process_commands(message)
Which would result in :
If you do not intend this type of 'recursive' behavior from your bot then adding a check to see if the bot is triggering to its own message would help :
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content == "hi":
await message.channel.send("hi")
await bot.process_commands(message)
Also, you could do something like this :
#bot.listen("on_message")
async def check_if_contains(msg):
if msg.author == bot.user:
return
if msg.content == "Hi":
await msg.channel.send("Hello there!")
Which would eliminate the need to load commands seperately.
on_message takes only one argument - message. You should also use message instead of ctx in process_commands() and if you are using the client variable then use client.process_commands() not bot.process_commands().
#client.event
async def on_message(message):
if message.content == "hi":
await message.channel.send('hi')
await client.process_commands(message)
And to avoid the bot taking messages from itself which would result in sending "hi" over and over again use this:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == "hi":
await message.channel.send('hi')
await client.process_commands(message)
You also have to enable intents.messages!

how to i import/use ctx for a python discord bot?

im making a python discord bot in replit and i cant get ctx to work
i tried import ctx and i just get an error message like AttributeError: module 'ctx' has no attribute 'message' heres my code:
import discord
import ctx
import os
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(""):
print(message.content)
guild = ctx.message.guild
await guild.create_text_channel('cool-channel')
client.run(os.getenv('TOKEN'))
my_secret = os.environ['TOKEN']
If you want to use the ctx parameter, you're supposed to use the commands extension for discord.py. In the on_message event you can use the message parameter "as ctx parameter".
#client.event
async def on_message(message):
if message.author != client.user:
if message.content.startswith(""):
print(message.content)
guild = message.guild
await guild.create_text_channel('cool-channel')
You should not need to use the ctx library to access the message variable's guild property. Try the following:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith(""):
print(message.content)
guild = message.guild
await guild.create_text_channel('cool-channel')

event() missing 1 required positional argument: 'coro' error discord.py

I'm making a bot for a new server and I keep getting this error, I'm fairly new to python and coding in general so any help is appreciated.
event() missing 1 required positional argument: 'coro' error discord.py
When I post this is saying it looks like my post is mostly code so im just going to type random things until it lets me post it. I have a pretty cool dog, he's very cute and he's the goodest boy to ever be good :)
Below is the code where I'm getting the error
#client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
And below is my full code
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 Sylas'):
await message.channel.send('Hello there!')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('>Is Dobbie cool?'):
await message.channel.send('Yes, he is the goodest boy!')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('>Hey Sylas'):
if str(message.author) in ["Trax#8949"]:
await message.channel.send('Hey Trax!')
#client.event
async def on_message(message):
if message.author == client.user:
return
async def on_message(message):
if message.content.startswith('>'):
async def ban(ctx, member : discord.Member, *, reason = None):
await member.ban(reason = reason)
client.run('my token :)')
Multiple on_message events will not work.
You can only have one event at a time, so you have to combine the events. This would look like this:
client = discord.Client() # Added brackets
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('>Hey Sylas'):
if str(message.author) in ["Trax#8949"]:
await message.channel.send('Hey Trax!') # First event
[Shortened]
if message.content.startswith('>ban'):
await member.ban(reason = reason) # Last event
You can have multiple client.command() "functions" but this does not count for events.
A post that explains it pretty well: Why multiple on_message events will not work
Try this:
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
elif message.content.startswith('>Hello Sylas'):
await message.channel.send('Hello there!')
elif message.content.startswith('>Is Dobbie cool?'):
await message.channel.send('Yes, he is the goodest boy!')
elif message.content.startswith('>Hey Sylas'):
if str(message.author) == "Trax#8949":
await message.channel.send('Hey Trax!')
elif message.content.startswith('>ban'): # You should restrict that to specific roles
await member.ban(reason = reason)
client.run('now its my token :)')
What I've done:
put all on_message in one on_message
changed to ">ban" instead of using an uncalled function in it
changed "in ['Trax']" to "== 'Trax'"
added elifs

Problem using when_mentioned with custom prefix

What im trying to do: I want my bot to send a message when its mentioned, i don't wanna use it as prefix just send a single message when mentioned.
My problem: It only reacts to the when_mentioned and will not react to the normal commands.
What i have tried:
#client.event
async def on_message(message):
if client.user.mentioned_in(message):
embed=discord.Embed(title=f"title)", color=0x7289da)
embed.set_thumbnail(url=thumbnailurl")
await message.channel.send(embed=embed)
try it:
#bot.event
async def on_message(msg):
if msg.mentions[0] == bot.user:
embed=discord.Embed(title="title",
description=f'My prefix is `{bot.get_prefix(msg)}`',
color=0x7289da)
embed.set_thumbnail(url="thumbnailurl")
await message.channel.send(embed=embed)
#client.event
async def on_message(message):
if message.mentions[0] == client.user:
embed=discord.Embed(title=f"title)", color=0x7289da)
embed.set_thumbnail(url=thumbnailurl")
await message.channel.send(embed=embed)
await client.process_commands(message)

Get message author in Discord.py

I am trying to make a fun bot for just me and my friends. I want to have a command that says what the authors username is, with or without the tag. I tried looking up how to do this, but none worked with the way my code is currently set up.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
##gets author.
await message.channel.send('You are', username)
client.run('token')
I hope this makes sense, all of the code i have seen is using the ctx or #client.command
The following works on discord.py v1.3.3
message.channel.send isn't like print, it doesn't accept multiple arguments and create a string from it. Use str.format to create one string and send that back to the channel.
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are {}'.format(message.author.name))
client.run('token')
Or you can just:
import discord
from discord import commands
client = commands.Bot(case_insensitive=True, command_prefix='$')
#client.command()
async def whoAmI(ctx):
await ctx.send(f'You are {ctx.message.author}')
If you want to ping the user:
import discord
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$WhoAmI'):
await message.channel.send('You are', message.author.mention)
client.run('token')

Categories

Resources