SyntaxError: 'await' outside async function discord.py - discord

import discord.ext
import discord
from discord.ext import commands
from discord.ext import tasks
from keep_alive import keep_alive
import os
import schedule
client = discord.Client()
client = commands.Bot(command_prefix="")
channel_id = 927272717227528262
channel = client.get_channel(channel_id)
#tasks.loop(seconds=1.0)
#client.event
async def on_message(message):
if message.author == client.user:
return
def job2():
await message.channel.send("message")
schedule.every(10).seconds.do(job2)
while True:
schedule.run_pending()
time.sleep(0)
keep_alive()
client.run(os.getenv('TOKEN'))
Every time I run it I get :
SyntaxError: 'await' outside async function
I am also having the issue where message is undefined the definition of job2 even though it was just defined in the line above.

You should probably be using discord.ext.tasks. Async functions aren't really supported by schedule.
In fact, your bot can never run because your schedule.run_pending() in the infinite loop will never end.
In order to keep your message there you'll need to put it in a global of some type.
the_message = None
#tasks.loop(seconds=1.0)
#client.event
async def on_message(message):
global the_message
if message.author == client.user:
return
the_message = message
async def job2():
if the_message is None:
return
await the_message.channel.send("message")
#tasks.loop(seconds=10.0) # you can do other things like `minutes=5` or `hours=1.75`
async def my_task():
await client.wait_until_ready()
await job2()
my_task.start()
# then the bot runs fine after
keep_alive()
client.run(os.getenv('TOKEN'))
Be careful that this might run before your client is not ready and initializing. You may also want to check wait_until_ready if you're going to do anything with the API, like in your case. You can also find other examples here.

await is used in asynchronous functions to wait for the function to do its thing. However, you have used it in a synchronous function (def job2)
message is not a variable. message is the parameter of the on_message function. A parameter can only be used in the function it is provided to.
Hope this answered your question.

Related

An error occurred when trying to async def discord.py without a command. How to perform async def?

How do I perform functions without a command and why do I get this error?
from discord.ext import commands
import json
import discord
import asyncio
import pathlib
from pathlib import Path
client = discord.Client(command_prefix="!", intents=discord.Intents.all())
dir_path = pathlib.Path.cwd()
path_class_components = Path(dir_path, 'class_app', 'class_components')
path_class_components = str(path_class_components).replace("\class_wark\class_app","")+"\config.json"
config = json.load(open(path_class_components, 'r'))
async def my_background_task():
await client.wait_until_ready()
counter = 0
channel = client.get_channel(id=970589128355905539) # replace with channel_id
while not client.is_closed():
counter += 1
await channel.send(f"{counter}")
await asyncio.sleep(60)
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.loop.create_task(my_background_task())
client.run(config['token'])
gives me an error... how do I fix it or what code should I use I don't understand at all)
Traceback (most recent call last): File "C:\Users\Monik\PycharmProjects\DiscordBuy\class_app\class_wark\class_requests.py", line 36, in <module> client.loop.create_task(my_background_task()) File "C:\Users\Monik\PycharmProjects\DiscordBuy\venv\lib\site-packages\discord\client.py", line 108, in __getattr__ raise AttributeError(msg) AttributeError: loop attribute cannot be accessed in non-async contexts. Consider using either an asynchronous main function and passing it to asyncio.run or using asynchronous initialisation hooks such as Client.setup_hook
Tasks are a great way to have functions/tasks executing every X period of time. Have a look at the docs and give them a go - shouldn't be too tricky to implement for your use case.
Thank you very much #ESloman , after reading the documentation a little and breaking my head, something came out
from discord.ext import tasks, commands
index = 0
def cog_unload(self):
self.printer.cancel()
#tasks.loop(seconds=1.0)
async def printer():
global index
print(index)
index += 1
#client.event
async def on_ready():
await printer.start()
client.run(config['token'])

Commands not working after updating to discord.py 2.0

I was trying to change my version from 1.7.3 to 2.0+ using pip install -U git+https://github.com/Rapptz/discord.py. Usually when I did this there would be an error about intents that would pop up, but instead there were no errors but it said:
discord.client logging in using static token
2022-12-20 12:34:14 INFO
discord.gateway Shard ID None has connected to Gateway (Session ID:6837cbf3ad28b9040ceb5e044dffe90f).
After that, any commands that I used didnt get responded to, as it worked perfectly fine in 1.7.3.
My code:
import discord, os, requests, json, random, time, datetime
from discord.ext import commands
from replit import db
import urllib
import asyncio
from discord import Member
from discord.ui import Select,Button,View
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="e!",
intents=intents)
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('whats the meaning of life'):
await message.channel.send(
'The meaning of life is "freedom from suffering" through apatheia (Gr: απαθεια), that is, being objective and having "clear judgement", not indifference. - Wikipedia.'
)
time.sleep(2)
await message.channel.send(
'There, someone just explained your life. Kind of depressing, isnt it?'
)
basically you don't have the correct intents to be able to send message with the bot
you need to have the discord.Intents.all()
and please don't use the time.sleep in an asynchronous function use asycio.sleep()
import asyncio
bot = commands.Bot('e!',intents=discord.Intents.all())
#bot.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('whats the meaning of life'):
await message.channel.send(
'The meaning of life is "freedom from suffering" through apatheia (Gr: απαθεια), that is, being objective and having "clear judgement", not indifference. - Wikipedia.'
)
await asyncio.sleep(2)
await message.channel.send(
'There, someone just explained your life. Kind of depressing, isnt it?'
)

Making my discord bot listen for specific messages and answer back when it identifies one

So, I'm new to python and even more to coding bots on discord. At the moment I'm using discord.py and red-bot (https://docs.discord.red/en/stable/index.html).
At the moment I'm trying to make the bot listen to a new message and print something in response to it, but I just can't figure it out. Since I'm using red-bot I didn't go through the steps of using client = discord.Client() and setting a token on the code itself so using #client.event() doesn't seem to work, nor #bot.event(), and I can't really find any other way to make the bot listen for an on_message() event.
Edit with part of my code:
import discord
from discord.ext import commands
from redbot.core import commands
class MyCog(commands.Cog):
client = discord.Client()
def __init__(self, bot):
self.bot = bot
#bot.event()
async def on_message(self, message):
if 'test' in message.content:
await self.send_message(message.channel, 'it works!')
Console returns that bot in #bot.event() is not defined.
Also a separate initialization file, it's done this way to follow Red's guide of how to make a cog.
from .remind import MyCog
def setup(bot):
bot.add_cog(MyCog(bot))
Here is an example of a cog that replies to a specific word mentioned. It also has a cooldown to prevent the bot spamming which I find absolutely neccesary to include.
from curses.panel import bottom_panel
import discord
from discord.ext import commands
class brilliant(commands.Cog):
def __init__(self, bot):
self.bot = bot
self._cd = commands.CooldownMapping.from_cooldown(1, 60.0, commands.BucketType.member) # Change accordingly
# rate, per, BucketType
def ratelimit_check(self, message):
"""Returns the ratelimit left"""
bucket = self._cd.get_bucket(message)
return bucket.update_rate_limit()
#commands.Cog.listener()
async def on_message(self, message):
if message.author == self.bot.user:
return
msg = message.content.lower()
brilliant = ['brilliant', 'Brilliant', 'brilliant!', 'Brilliant!']
if any(word in msg for word in brilliant):
retry_after = self.ratelimit_check(message)
if retry_after is None:
await message.channel.send("Brilliant!")
await self.bot.process_commands(message)
else:
return
async def setup(bot):
await bot.add_cog(brilliant(bot))
If you want it in the main file, you can put something a lot simpler:
brilliant = ['brilliant', 'Brilliant', 'brilliant!', 'Brilliant!']
#bot.event
async def on_message(message):
if message.author == bot.user:
return
msg = message.content.lower()
if any(word in msg for word in brilliant):
await message.channel.send("Brilliant!")
await bot.process_commands(message)
Hope it helps.

#client.command not running, discord.py [duplicate]

Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")

'Member' object has no attribute 'hasPermission'

i wanted to make a bot to discord for delete messages easly. it works 3 months ago but now i got an error " 'Member' object has no attribute 'hasPermission' ". thanks for everyone to share opinion. have a nice day.
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
if message.content.startswith('botcuksil'):
if (message.author.hasPermission('MANAGE_MESSAGES')):
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
It is throwing 'Member' object has no attribute 'hasPermission' this error because 'Member' does not have an attribute named 'hasPermission'. To fix your problem, I have re-written your code and also given some explanation below the code:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
bot = commands.Bot(command_prefix = 'botcuk')
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Streaming(name="admin biseyler deniyor", url='https://www.youtube.com/watch?v=fw7L7ZO4z_A'))
#client.event # we need to add and check for a client event
async def on_message(): # we need to execute the command when we recieve an message so we define a function called 'async def on_message():'
if message.content.startswith('botcuksil'):
if message.author.guild_permissions.manage_messages: # we need 'author.guild_permissions.(permission)' intead of 'if (message.author.hasPermission('MANAGE_MESSAGES'))'
args = message.content.split(' ')
if len(args) == 2:
if args[1].isdigit():
count = int(args[1]) + 1
deleted = await message.channel.purge(limit = count)
await message.channel.send('{} mesaj silindi'.format(len(deleted)-1))
I have not tested this yet, but according to me, your problem: 'Member' object has no attribute 'hasPermission' should be solved.
What you have done was message.author.hasPermission('MANAGE_MESSAGES') but message.author has no attribute like hasPermission.
Summary of what I have done:
I have added a client event (#client.event) and an async function on_message().
What it does is it checks if it is getting an message or not. If it detects a new message in any of the servers it is invited to, it triggers the code in on_message().
As I said: What you have done was message.author.hasPermission('MANAGE_MESSAGES') but message.author has no attribute like hasPermission.
Instead of writing that, we can use message.author.guild_permissions.manage_messages. It checks if the message author's server's permissions include "manage_messages". Hope my solution fixes your problem.
Always happy to help!
-sqd mountains

Resources