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.
Related
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('token is here')
I'm trying to create a discord bot and I'm trying to use this simple code to get started but whenever I type the command ($hello) in a server that the bot is in it doesn't respond. Any solutions?
I'm assuming you're new to discord.py which is fine, though writing code like this is a little counter-intuitive. Instead of registering it as an event, why not just register it as an actual command?
This is how it should be done:
from discord.ext import commands
client = commands.Bot(command_prefix='$') # This can always be changed
#client.event()
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.command()
async def hello():
await ctx.send('Hello')
client.run('token is here')
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'))
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)
So I'm making a basic bot command that responds with what the player said, like doing !test code will make the bot respond with 'code'. For some reason, nothing happens when the command is run. I even put a print inside of it to see if it was actually being run, and it wasn't. Here's my code:
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#bot.command()
async def test(ctx, arg):
await ctx.send(str(arg))
client.run('token here')
Any help is appreciated.
try this out:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#client.command()
async def test(ctx, *, arg):
await ctx.send(str(arg))
client.run('token here')
heres what you got wrong:
client = discord.Client()
bot = commands.Bot(command_prefix="!")
You had 2 separate handlers for the bot, if you use commands you only need the bot = commands.Bot(command_prefix="!") line, in this case you had the bot handler for commands but you were running client
The code below runs as expected when tested using Python3.7 ...
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#client.command()
async def test(ctx, *arg):
await ctx.send(str(arg))
client.run('token here')
Your code as posted has an await statement outside of a function
......
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
.......
Also change
#bot.command()
async def test(ctx, arg):
TO:
#bot.command()
async def test(ctx, *arg):
For an explanation as to why you pass *arg and not arg:
args-and-kwargs
You can't run client and bot together if want bot to run the last line of code must be bot.run('your token') if you want client to run use client.run('your token')