When ever I run the code I get an error with async, I have imported asyncio to attempt to fix this but the errors keep flooding in
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = command.Bot(command_prefix="=")
#bot.event
async def on_ready():
print ("Bot Onine!")
print ("Hello I Am " + bot.user.name)
print ("My ID Is " + bot.user.id)
The error I get:
async def on_ready():
^
SyntaxError: invalid syntax
Please if anyone knows a fix
To use the async keyword you need python 3.5 or above. If you're using python 3.4 or below you can't use async.
the alternative is to decorate the method with coroutine. This code
#bot.event
async def on_ready():
...
Becomes:
#bot.event
#asyncio.coroutine
def on_ready():
....
Since discord events already need the bot.event decorator, discord.py api provides the async_event decorator to do both the coroutine and the event decoration in a single call, so you can also write it like this:
#bot.async_event
def on_ready():
....
In the same vein, you can't use await either, so you must use yield from instead.
await bot.say('Some response')
becomes
yield from bot.say('Some response')
Related
So I have this simple discord bot written in python and when I try some commands, it gives me "on.message () takes 0 positional arguments but 1 was given". Here's the code of the bot.
import discord
import os
import requests
import json
from keep_alive import keep_alive
client = discord.Client ()
#client.event
async def on_ready () :
await client.change_presence(activity=discord.Game('Sivo nebo'))
print ('We have logged in as {0.user}'.format(client))
#client.event
async def on_message () :
if message.author == client.user :
return
if message.content.startswith('!hi') :
await message.channel.send('Hi!')
if message.content.startswith('!goodbye') :
await message.channel.send('Goodbye!')
keep_alive()
client.run(os.getenv('TOKEN'))
All events take an implicit context argument.
async def on_ready(ctx):
...
async def on_message(ctx):
...
Try to use async def on_message(message):
I'm quite sure that your current code won't work because message is undefined.
If you view the discord.py documentation, you'll see that the on_message() event reference implicitly takes on argument: message. So when you define on_message with not parameters, discord.py attempts to pass message, but is not able to.
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hi') :
await message.channel.send('Hi!')
if message.content.startswith('!goodbye') :
await message.channel.send('Goodbye!')
On a side note, I would recommend checking out the discord.ext.commands framework, which allows you to create commands and is better than using message.content.startswith() and variations of that.
I am trying to make a discord bot change to online status
I have been using pycharm and terminal. I have tried reordering the code multiple different ways, but here is what I have right now
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
#client.event
async def on_ready():
print('Bot is ready.')
await client.run(bot token)
I also tried these:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
#client.event
async def on_ready():
print('Bot is ready.')
await client.run(bot token)
on_ready
and
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
#client.event
async def on_ready():
print('Bot is ready.')
await client.run(bot token)
on_ready
I got these errors:
:8: RuntimeWarning: coroutine 'on_ready' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
and
SyntaxError: 'await' outside function
Please help me get the bot online
Firstly, await cannot be outside of an async def function, the thing your awaiting is not indented enough to be inside the function on_ready.
Secondly, you shouldn't try to call on_ready manually, once the bot runs, it'll call on_ready itself.
Thirdly, Never put client.run inside of on_ready! Instead put it at the end of the file, if you do put it inside of on_ready, it'll never run.
so, this would be the ideal code:
#client.event
async def on_ready():
print('Bot is ready!')
client.run(TOKEN)
And as for storing your bot token, i would put it inside a database, like the replit or mongoDB database.
the await function is not needed for client.run()
put
async def on_ready():
print("Bot is ready.")
and put client.run at the end of the file. In my case, I use a file called config.py in which I have declared the value of token
token = "Enter your token here"
to import the variable, I did imported config with import config and since it's in config file, I entered client.run(config.token)
The print function needs to be intended another 4 spaces to be like:
async def on_ready():
print("Bot is ready.")
Try to install asyncio if the problem still occurs
I've started to try coding a discord bot but it was cut short when setting a prefix and events wouldn't work. It just displays an error message that says 'the command "hello" can not be found'.
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix="*" ,status=discord.Status.idle, activity=discord.Game("Starting up..."))
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.do_not_disturb, activity=discord.Game("Preparing Puzzle Event"))
print("Bot is running.")
#client.event
async def hello(ctx):
await ctx.send("Hi")
client.run('[The token]')
I know that the token is valid as I've ran it before without assigning a prefix and using 'async'.
#client.event
async def hello(ctx):
await ctx.send("Hi")
This won't work because hello is not a built-in event. See: https://discordpy.readthedocs.io/en/latest/api.html#discord-api-events
What you want is a command probably
#client.command()
async def hello(ctx):
await ctx.send("Hi")
This registers the bot to respond to the command .hello assuming . is your prefix
I have this piece of code:
from discord.ext import commands
#bot.command()
#commands.max_concurrency(1,per=commands.BucketType.default,wait=False)
async def function(ctx,arg1,arg2,arg3):
the max_concurrency() works, but when the max concurrency is met, i want it to send a message to the author that says the bot is busy, I've tried the MaxConcurrencyReached exception handle but it simply doesn't work, anyone know how to work with this command?
I figured it out.
After defining the function I want to put the max concurrency limit:
from discord.ext import commands
#bot.command()
#commands.max_concurrency(1,per=commands.BucketType.default,wait=False)
async def function(ctx,arg1,arg2,arg3):
Then you have to handle it with on_command_error() and isinstance(). For example:
#bot.event
async def on_command_error(ctx,error):
await ctx.message.delete()
if isinstance(error, commands.MaxConcurrencyReached):
await ctx.author.send('Bot is busy! Please retry in a minute')
return
So, I have a Python program using discord.py where I am trying to have functions manage different message commands, but when I put the "await" in the function, it gives me a syntax error because it's not in the async. Any way I can get around this?
def selectedCommand(message):
await client.send_message(stuff in here)
#client.event
async def on_message(message):
selectedCommand(message)
#client.event
async def on_edit_message(message):
selectedCommand(message)
You need to make selectedCommand a coroutine (an async def function) as well, and then await it when it's called:
async def selectedCommand(message):
await client.send_message(stuff in here)
#client.event
async def on_message(message):
await selectedCommand(message)