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)
Related
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!
command I tried:
#bot.event
async def on_message(message):
if message.channel.server.id == '783763061907527':
if message.content.startswith('!test'):
await message.channel.send('this is a test')
why it isn't working?
server was renamed to guild in discord.py 1.0+. Also IDs are integers, not strings.
#bot.event
async def on_message(message):
if message.guild.id == 783763061907527:
if message.content.startswith('!test'):
await message.channel.send('this is a test')
Guild, Channel, Message or User's id is an integer. And startswith is a function and using like in below.
#bot.event
async def on_message(message):
if message.guild.id == 783763061907527:
if message.content.startswith('!test'):
await message.channel.send('this is a test')
So I was trying to make embed command. Code:
#client.command()
#commands.has_permissions(administrator=True)
async def embed(ctx):
def check(message):
return message.author == ctx.author and message.channel == ctx.channel
await ctx.send('Send me your source code.')
source = await client.wait_for('message', check=check)
src = source.content
await ctx.send(embed=src)
And yeah I am wondering is there a way to convert this string to discord.Embed. I really want to send embed using source code.
What you got is a string which you can't send as an embed like this.
As you said, you have to put this into an embed.
#client.command()
#commands.has_permissions(administrator=True)
async def embed(ctx):
def check(message):
return message.author == ctx.author and message.channel == ctx.channel
await ctx.send('Send me your source code.')
source = await client.wait_for('message', check=check)
embed = discord.Embed(title="Custom Embed", description=source.content, colour=discord.Colour.red())
# You can put the colour to whatever you want with 0xHEXCODE (e.g. 0x30F90)
await ctx.send(embed=embed)
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
How to create a Discord.py command?
In fact, it doesn't show anything in the console at all, what should I do?
There's no error, nothing is executing.
import discord
import time
from discord.ext import commands
client = commands.Bot(command_prefix='/')
# This detects when the bot started.
#client.event
async def on_ready():
print(f'{client.user.name} is ready!')
# This detects when someone sends a message.
#client.event
async def on_message(message):
if message.author == client.user:
return
if 'fuck' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'shit' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'bitch' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot swear in this discord server!')
if 'https://' in message.content:
await message.delete()
async with message.channel.typing():
time.sleep(1)
await message.channel.send('You cannot send links in this discord server!')
# This cteates a command.
#commands.command()
async def test(ctx, arg):
async with message.channel.typing():
time.sleep(1)
await ctx.send(arg)
client.add_command(test)
client.run('TOKEN')
You need to add client.process_commands at the end of the on_message event.
async def on_message(message):
# ...
await client.process_commands(message)
Another thing wrong in your code is that you're not defining message in the test command, you can fix it by using the Context.message attribute
async with ctx.message.typing():
# ...
# Or
async with ctx.typing():
# ...
Also you can simply use the client.command decorator instead of commands.command so you don't need to add the line client.add_command
#client.command()
async def test(ctx, arg):
# ...
Reference:
Bot.process_commands
Context.message
Context.typing