import discord
import asyncio
client = discord.Client()
#client.event
async def on_ready():
print ("I’m Now Online")
#client.event
async def on_message(message):
if message.author == client.user:
return
elif message.content.startswith("deletethis"):
I’m wonder how I could be able to add to this to delete the above command when the author of the message sends the command above.
May someone help create one? I’ve tried my self by brain and didn’t find anything online so I’m looking for some help maybe.
for async just do
await client.delete_message(message)
otherwise for rewrite just do
await message.delete()
Finished code:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("deletethis"):
await asyncio.sleep(1)
await message.delete()
Related
I have tried to search the web for ways to do this, but I've had no luck. I know this is seems very easy, but I am just starting with discord.py and I'm looking for some help. Thanks.
You can add this to your bot code.
#client.event
async def on_message(message):
if message.content == ‘verify’:
await message.channel.send(“A message”)
Here
#client.event
async def on_message(message):
if message.content == "verify":
#Your code
import discord
token = '<insert-token-here>'
# Make sure to enable the Message Content intent in the Discord developer portal
client = discord.Client(intents=discord.Intents.all())
#client.event
async def on_ready():
print(f"Logged on as {client.user}")
#client.event
async def on_message(message: discord.Message):
if message.author == client.user:
# skip message from self
return
print(f"Message: {message.content}")
await message.channel.send(message.content)
if __name__ == '__main__':
client.run(token)
I added the bot status and after that the
Commands don't work. Added a answerers answer but it still no work ( help works not but not hello ;-;)
import discord
from KeepAlive import keep_alive
client=discord.Client()
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.online,activity=discord.Game('Hey There! Do €help to start!'))
print('We have logged in as {0.user}'.format(discord.Client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await bot.process_commands(message)
keep_alive()
client.run('wont say token :)')
If you are talking about commands and not "commands" that you run with on_message then you have to add await client.process_commands(message) (check this issue in documentation). If your on_message event is not working then it's probably only because of missing _ in on_message event.
#client.event
async def on_message(message): # your forgot "_"
if message.author == client.user: # discord.Client won't work. Use client.user instead
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
if message.content.startswith('$help'):
await message.channel.send('no help here!')
await client.process_commands(message) # add this line at the end of your on_message event
the problem is your message function, it has async def onmessage(message): but the correct one is:
#client.event
async def on_message(message):
And I recommend defining the prefix and then separating it from the message so you don't have to keep typing $ in every if, and save the elements written after the command for future functions:
PREFIX = "$"
#client.event
async def on_message(message):
msg = message.content[1:].split(' ')
command = msg[0]
if command == "hello":
await message.channel.send('Hello!')
I have some problem when I try to create a bot
Here is a part of my code:
#client.event
async def on_message(message):
if message.author == client.user:
return
await client.process_commands(message)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
general_channel = client.get_channel(874180398475804695)
await general_channel.send('I am ready',delete_after=10)
#client.event
async def on_message(message):
if any(word in message.content for word in rude_word):
await message.reply('Hey man, that word is not allowed here .',delete_after=2)
await message.delete()
#client.command(name='ping')
async def ping(message):
await message.reply('pong')
When I type command, the bot does not work.
BUT, if I remove the part of:
#client.event
async def on_message(message):
if any(word in message.content for word in rude_word):
await message.reply('Hey man, that word is not allowed here .',delete_after=2)
await message.delete()
The command works again.
Another situation, I remove :
#client.command(name='ping')
async def ping(message):
await message.reply('pong')
The bot still can works(delete message). However, when I combine these two, the #client.command doesn't works. I have try my best to figure it out but still have no idea. What can I do to let the commands work?
The issue is with on_message. You need to add await client.process_commands(message) at the end of on_message as mentioned here in the discord.py FAQ.
import discord
import os
import requests
import json
import random
from random import randint
from replit import db
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print(f'{client.user.name} működik!')
await client.change_presence(activity=discord.Game(name='egy bot vagyok'))
#client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f' {member.name}, itt van!'
)
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
print(f'haha')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('ping'):
await message.channel.send("pong")
if message.content.startswith("shrug"):
await message.channel.send('¯\_(ツ)_/¯')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('.pic'):
await message.channel.send(file=discord.File('combo-0.png'))
token = os.environ.get("secret")
client.run(token)
im a beginner in discord bot programming
this is my whole code for my discord bot and all of the commands are not working except one
te .pic command
all of the commands worked until now and i dont know why they dont work
if i would get some help i would be happy :D
(sorry for my bad english, english isnt my first language)
You cannot have multiple on_message events, only the last one will be registered. You have to combine them into one
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('ping'):
await message.channel.send("pong")
if message.content.startswith("shrug"):
await message.channel.send('¯\_(ツ)_/¯')
if message.content.startswith('.pic'):
await message.channel.send(file=discord.File('combo-0.png'))
You can't mix up the discord.Client() and commands.Bot clients like this.
commands.Bot is a subclass of discord.Client, so you should be able to do everything that you could do with discord.Client() with commands.Bot()
bot = commands.Bot('!')
#bot.event
async def on_ready():
print('ready')
#bot.command()
async def ping(ctx):
await ctx.send('pong')
For help with the rewrite discord.ext.commands, there are a butt ton of tutorials on the internet.
What I have so far, where the #s are are where I might want to place them. of course, in your answer please pick a different location.
The top is cut off:
#client.event
async def on_ready():
print('Bot is open {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
#status thing here?
if message.content.startswith('gf!ami'):
await message.channel.send('Hello!')
if message.content.startswith('gf!help'):
await message.channel.send(h)
if message.content.startswith('gf!SEhelp'):
await message.channel.send(sh)
if message.content.startswith('gf!music'):
await message.channel.send
("https://open.spotify.com/playlist/49ddWzhNmWxyMuYlvu8L5Z?si=jYlh6zK9RryKSeMlvKZ3yA")
if message.content.startswith('gf!d6'):
await message.channel.send(d6())
if message.content.startswith('gf!cl'):
await message.channel.send(cl)
#Status thing here?
client.run('bot token')
Check out documentation for presence updating https://discordpy.readthedocs.io/en/v0.16.12/api.html#discord.Client.change_presence
For example, you should make something like that:
await client.change_presence(game=discord.Game(type=1, url="https://www.twitch.tv/tomori_bot", name="Follow me on twitch ^_^"))
You can see full example there: https://github.com/TomoriBot/Tomori/blob/master/tomori.py
at statuses() function