#client.command()
async def spam(ctx):
while True:
time.sleep(2)
await ctx.send("spam")
#client.event
async def on_message(message):
if message.content == "!stop":
# stop spamming
I want the code to stop spamming but I'm not sure how.
Try this example using wait_for:
#client.command()
async def spam(ctx):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
stopped = False
await ctx.send("spam")
while not stopped:
try:
message = await client.wait_for("message", check=check, timeout=2)
stopped = True if message.content.lower() == "!stop" else False
except asyncio.TimeoutError: # make sure you've imported asyncio
await ctx.send("spam")
await ctx.send("spam finished!")
References:
Client.wait_for()
Message.author
Message.channel
Related
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'Hey Sloime':
if message.author.id == xxx:
await message.channel.send('Yes master?')
elif message.author.id == xxx:
await message.channel.send('Tell me brother')
else:
await message.channel.send('TF u want?')
else:
return
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'Can I have my Dis ID?':
await message.channel.send(message.author.id)
else:
await message.channel.send('Do I look like I can answer that?')
return
client.run(os.environ['TOKEN'])
It's my first time ever coding in Python, I have experience coding in c, c++, java and sql. My problem is that after I enter the second event on this bot I want to wait for a message corresponding to the first event, but it keeps looping on the second event, I apologize in advance if this is very simple.
Something like this?
#client.event
async def on_message(message):
if message.content == 'i want cheese':
await message.channel.send('how much?')
msg = await client.wait_for('message')
await message.channel.send(f'ok, you want {msg.content} cheese')
# you can do a check like this also
msg = await client.wait_for('message', check=lambda msg: msg.author.id == xxx)
wait_for('message') waits for a message to happen
client = commands.Bot(command_prefix = '!')
stop_var = False
#client.event
async def on_ready():
print('Bot is ready.')
#client.command()
async def hello(ctx):
while (stop_var==False):
await asyncio.sleep(1)
await ctx.send('Hello!')
if stop_var:
break
#client.command()
async def stop(ctx):
await ctx.send('Stopped!')
stop_var = True
I'm trying to make it so when I type !stop, the !hello command loop ends, but my code does not work.
This seems to work:
#client.command()
async def hello(ctx):
stop_var = False
while (not stop_var):
await asyncio.sleep(1)
await ctx.send('Hello!')
def check(msg: discord.Message):
return not msg.author.bot and msg.content.lower() == "!stop"
try:
if await client.wait_for("message", check=check, timeout=1.5):
await ctx.send("Stopped")
stop_var = True
except asyncio.TimeoutError:
print("no stop")
Also if you have overridden on_command_error event you can use this to avoid triggering it:
#client.command()
async def hello(ctx):
stop_var = False
while (not stop_var):
await asyncio.sleep(1)
await ctx.send('Hello!')
def check(ctx):
return not ctx.author.bot and ctx.command.name == "stop"
try:
if await client.wait_for("command", check=check, timeout=1.5):
stop_var = True
except asyncio.TimeoutError:
print("no stop")
#client.command()
async def stop(ctx):
await ctx.send("Stopped")
I watched a tutorial on how to get Input from users in Discord.py. The bot however, does not respond to the input given to it.
The bot sends the "This is a bot test. Type (YES/NO)" message. But when I type "yes" or "no" it does not respond.
#client.command()
async def bot(ctx):
await ctx.channel.send("This is a bot test. Type (YES/NO)")
try:
message = await bot.wait_for("message", check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=30.0)
except asyncio.TimeoutError:
await ctx.channel.send("I have been ignored")
else:
if message.content.lower() == "yes":
await ctx.channel.send("The test was succesfull!")
elif message.content.lower() == "no":
await ctx.channel.send("Thank you for your response")
else:
await ctx.channel.send("I cannot understand you")
You're decorating with client but calling bot in your function.
Change
#client.command()
async def bot(ctx):
await ctx.channel.send("This is a bot test. Type (YES/NO)")
try:
message = await bot.wait_for("message", check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=30.0)
To
#client.command()
async def bot(ctx):
await ctx.channel.send("This is a bot test. Type (YES/NO)")
try:
message = await client.wait_for("message", check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=30.0)
Hey so when I type $opgg or $ftgopgg it will not come up with anything on the following
#bot.event
async def on_message(message):
if message.content('$opgg'):
response = 'Spend me the names'
await message.channel.send(response)
def check(m):
return m.author.id == message.author.id
opgg = await bot.wait_for('message')
await message.channel.send(f'https://oce.op.gg/multi/query={opgg.content.replace(" ", "%20")}')
print("done")
#bot.event
async def on_message(message):
if message.content == '$ftgopgg':
teamopgg = "https://oce.op.gg/multi/query=Disco Inferno, spranze, killogee, blank76, reeks"
await message.send(teamopgg.replace(" ", "%20"))
print("Tdone")
it was working like an hour ago but I could not use both commands, I don't know if I've gotten code work or if there is a mistake
You have 2 on_message events but discord.py supports only one.
If you are trying to create commands for your bot I recommend use #client.command() decorator instead of listening on_message event.
Something like:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="$", intents=discord.Intents.all())
#bot.command()
async def opgg(ctx):
response = "Spend me the names"
await message.channel.send(response)
def check(m):
return m.author.id == message.author.id
#bot.command()
async def ftgopgg(ctx):
teamopgg = "https://oce.op.gg/multi/query=Disco Inferno, spranze, killogee, blank76, reeks"
await message.send(teamopgg.replace(" ", "%20"))
print("Tdone")
bot.run("TOKEN")
If you want to use bot.command() and on_message event at the same time, you can use :
#bot.listen()
async def on_message(message):
instead of:
#bot.event
async def on_message(message):
Simple fix all i had to do was remove the lower #bot.event and async def on_message(message)
new code:
#bot.event
async def on_message(message):
if message.content('$opgg'):
response = 'Spend me the names'
await message.channel.send(response)
def check(m):
return m.author.id == message.author.id
opgg = await bot.wait_for('message')
await message.channel.send(f'https://oce.op.gg/multi/query={opgg.content.replace(" ", "%20")}')
print("done")
if message.content == '$ftgopgg':
teamopgg = "https://oce.op.gg/multi/query=Disco Inferno, spranze, killogee, blank76, reeks"
await message.send(teamopgg.replace(" ", "%20"))
print("Tdone")
So I'm making bot in python and I don't know how to make that other user can write -join and then bot will trigger the commend. My code currently is in cogs so I'm using #commands.command instead #bot.command. Here is my code:
class GamesCog(commands.Cog, name="Games"):
def __init__(self, bot):
self.bot = bot
#commands.command()
async def game(self, ctx):
await ctx.send("Find a second person to play dice or play against the bot. (-join/-bot)")
await ctx.send(file=discord.File('game.jpg'))
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel and msg.content.lower() in ["-join", "-bot"]
msg = await self.bot.wait_for("message", check=check)
def setup(bot):
bot.add_cog(GamesCog(bot))
print('File games.py is ready')
I know that in the return msg.author author shouldn't be there, I just don't know what to replace it with.
You're actually doing nothing with the message, you're waiting for it, and then nothing
msg = await self.bot.wait_for("message", check=check)
if msg.content.lower() == "-join":
# Do something
elif msg.content.lower() == "-bot":
# Do something
def check(msg):
return msg.channel == ctx.channel and msg.content.lower() in ["-join", "-bot"]