#client.event
async def on_message(message):
await client.process_commands(message)
I have no idea how to use this.
The above code is totally fine. If you haven't DEFINED Your bot yet, You have to do it.
Here is the simple code you can modify according to your needs.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = 'YOUR PREFIX')
#bot.command()
async def ling(ctx):
await ctx.send("Mabel")
Related
I've never coded before so I'm pretty new and I'm trying python on replit, I've searched a lot and this is what I did so far but it isn't working. (ignore the reverse part)
import os
import discord
from keep_alive import keep_alive
client = discord.Client(intents=discord.Intents.default())
#client.event
async def on_ready():
print("I'm in")
print(client.user)
#client.event
async def on_message(message):
if message.content.startswith("!reverse"):
await message.channel.send(message.content[::-1])
my_secret = os.environ['DISCORD_BOT_SECRET']
client.run(my_secret)
keep_alive()
my_secret = os.environ['DISCORD_BOT_SECRET']
client.run(my_secret)
async def on_message(message):
echo = message.content.split(" ", 1)[1]
if message.content.startswith("!say"):
await message.channel.send(echo)
I want the bot to be like this:
me:!say blah blah blah
bot: blah blah blah
thanks to anyone that answers
There's a lot of problems here
Intents.default() doesn't include the message contents intent, so you won't be able to read messages. For more info on intents and how to enable them, read the docs: https://discordpy.readthedocs.io/en/stable/intents.html
You've got two on_message functions, which doesn't work. You can't have multiple functions with the same name. Combine them into one instead.
Never put any code underneath client.run() - it'll never get executed.
You've got two client.run()'s. Why?
The on_message at the bottom is missing the #client.event decorator, so even if you wouldn't have 2 of them it still wouldn't be invoked.
Why don't you use a Bot with commands instead of manually parsing everything in on_message? https://discordpy.readthedocs.io/en/stable/ext/commands/index.html
Replit isn't made to run bots on and will cause you a lot of trouble. Consider hosting it on an actual VPS (or during the development phase - just locally).
Ok, there's some other changes to make first.
You're not using commands, and are instead looking for messages(technically nothing wrong with that, but it can cause unnecessary issues)
I'll modify the code, and hopefully, it works.
import os
import discord
from keep_alive import keep_alive
client= commands.Bot(command_prefix='!', intents=discord.Intents.default())
#client.event
async def on_ready():
print("I'm in")
print(client.user)
#client.command()
async def reverse(ctx,*,message):
await ctx.message.delete()
await ctx.channel.send(message[::-1])
my_secret = os.environ['DISCORD_BOT_SECRET']
client.run(my_secret)
keep_alive()
my_secret = os.environ['DISCORD_BOT_SECRET']
client.run(my_secret)
#client.command()
async def say(ctx, *, message):
await ctx.message.delete()
await ctx.channel.send(message)
I made this small discord bot to show a friend how to do it, exactly how I have done it before. But it doesnt answer my message in discord, and I cant find the error.
import discord
import os
client = discord.Client()
#client.event
async def on_ready():
print('Online as {0.user}'.format(client))
#client.event
async def in_message(message):
if message.author == client.user:
return
if message.content.startswith('Hello'):
await message.channel.send('Hello! {message.author.mention}')
client.run(os.getenv('TOKEN'))
Sorry if its obvious, I just cant see it.
Use on_message instead of in_message.
Format string 'Hello! {message.author.mention}' like f'Hello! {message.author.mention}'.
instead of using on_message events u can use
#client.command()
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}")
This is how u create actual Commands in discord.py
I have a discord bot with like two simple functions (client.event with client = discord.Client()) and recently I've been trying to work on commands for a music bot, but apparenly the bot doesn't even respond to the simplest commands there are. For example:
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.command()
async def hello():
print("Hello") #So when I type !hello in a channel I want "Hello" written in the console as prove that the bot is responding
#client.event
async def on_ready():
print("Online.") #Just simple if the bot is online
The function client.event works of course but the bot.command() function wont react in any way. Can anyone help me here? I am really about to break everything I own.
Try this:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def hello(ctx):
print("Hello")
#bot.event
async def on_ready():
print("Online.")
bot.run("Token")
Firstly, why are you defining client and bot simultaneously? Secondly you don't have bot.run() in your code. Thirdly you have to always give ctx in #bot.command.
You either use discord.Client or commands.Bot, you cannot use both of them. If you take a look at your code, you're decorating the command with bot.command(), but at the end of the file you're running client. If you want commands I suggest you use commands.Bot:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def hello(ctx):
print("Hello")
#bot.event
async def on_ready():
print("Online.")
bot.run("XXX")
I'm trying to switch my bot from using #client.event for everything to also using #client.command
The problem is when running the command .commandtest, nothing happens. I put a print statement in the code to see if the problem was writing in the discord channel, but it doesn't print to the terminal either.
from discord.ext import commands
import os
import requests
import asyncio
import ctx
from keep_alive import keep_alive
client = discord.Client()
client = commands.Bot(command_prefix=".")
client.remove_command('help')
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.command()
async def commandtest(ctx):
print("command worked?")
await ctx.send('this is a test for commandtext')
keep_alive()
client.run(os.getenv('TOKEN'))
The thing is I think we need to read docs, in there its written that on_message stops commands from working , So we should use await client.process_commands(message) At the end of client.event and it will work for sure
This should work:
import discord
client = discord.Client()
When, the bot is ready, it prints out that he's logged in
#client.event
async def on_ready():
print('Logged in as ' + str(client.user))
If you get a message in discord, that starts with '.', send the test message
#client.event
async def on_message(message):
if message.content.startswith('.'):
await message.channel.send('this is a test for commandtext')
client.run('YOUR TOKEN')
You do not need both client = discord.Client() and client = commands.Bot(command_prefix=".") You would have to delete client = discord.Client() for the command to work. Also, you do not need import ctx because it is already imported in from discord.ext import commands. That should be the problem with your code. If you run into any other problems you could reply to this thread.
I'm learning how to use the discord.py API and this example code (from here: https://discordpy.readthedocs.io/en/latest/quickstart.html) does not work. I think it has something to do with the async syntax, but I have no idea what could be wrong with it.
Any help would be appreciated!
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('private token that is filled in in the actual code')
On line 7, “async def on_ready():”, the error is an invalid syntax on async. I’m using 3.8.3
client = commands.Bot(command_prefix = '!') instead of client = commands.Client()
You should import commands too
import discord
from discord.ext import commands