I am getting a syntaxError stating that await is outside of the function. Can anyone help me with a fix/explanation?
import discord
import os
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!')
my_secret = os.environ('TOKEN')
client.run(os.environ('TOKEN'))
Related
Heres my code, it's only responding to the *b command even though the *m *h and *a commands are all pretty much the same. I checked the imagur links and its not that. I originally only had the *m command and it worked fine and then I added the other commands and it's just not responding to any of the commands but the *b command.
import discord
import os
import random
client = discord.Client()
anyphotos = [<just a bunch of imagure links in a list>]
mphotos = [<also just a bunch of imagur links in a list>]
bphotos = [<even more imagur links in a list>]
#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('*h'):
await message.channel.send("hello!")
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('*a'):
await message.channel.send(random.choice(anyphotos))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('*m'):
await message.channel.send(random.choice(mphotos))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('*b'):
await message.channel.send(random.choice(bphotos))
client.run('<token>')
You can only have one on_message but you can combine all of what you have into 1
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('*h'):
await message.channel.send("hello!")
if message.content.startswith('*a'):
await message.channel.send(random.choice(anyphotos))
if message.content.startswith('*m'):
await message.channel.send(random.choice(mphotos))
if message.content.startswith('*b'):
await message.channel.send(random.choice(bphotos))
This is my code but I cant get the loop to work.
import discord
from discord.ext import tasks, commands
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('!test'):
await message.channel.send('Working')
#tasks.loop(seconds = 10)
async def myLoop():
channel = client.get_channel(899698630271856640)
await channel.send('1')
To start a discord.py loop, you must run it under the on_ready function. Add myLoop.run() in the function.
I have this code that sends a message with 4 reactions, is there a way to take the first reaction a user inputs (it has to take only one input if the user chooses another option and it overrides the first one that is also fine) and save it as a variable to use later on?
import discord
import os
client = discord.Client()
some_list = []
msg = None
#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
msg = await message.channel.send("**Question 1?**")
reactions = ['1️⃣','2️⃣','3️⃣','4️⃣']
for emoji in reactions:
await msg.add_reaction(emoji)
#client.event
async def on_reaction_add(reaction, user):
if reaction.message == msg:
some_list.append(user)
client.run("token")
Thanks in Advance!
You should really use wait_for an example of how to use this can be found on my previous answer: Getting input from reactions not working discord.py
You should really use a dictionary if you don't want to go with wait_for.
import discord
import os
client = discord.Client()
some_dict = {}
msg = None
#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
msg = await message.channel.send("**Question 1?**")
reactions = ['1️⃣','2️⃣','3️⃣','4️⃣']
for emoji in reactions:
await msg.add_reaction(emoji)
#client.event
async def on_reaction_add(reaction, user):
if reaction.message == msg:
some_dict[user.id] = str(reaction.emoji)
client.run("token")
You can then access it using some_dict[user.id]
Im new to python so I cant see where I have made an error.
import discord
import os
from config import TOKEN
client = discord.Client()
#Sends a message in terminal if it has worked
#client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
#Sends a message if !test is included in a sent message
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!test'):
await message.channel.send('test')
#client.event
async def on_member_join(self, member):
guild = member.guild
if guild.system_channel is not None:
to_send = 'Welcome {0.mention} to {1.name}'.format(member, guild)
await guild.system_channel.send(to_send)
client.run(TOKEN)
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.