Discord.py bot only responding to one command - python

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))

Related

await outside function in python

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'))

Mod mail command

Someone know how to make the bot close the modmail? Like the member start spamming on the bot dms and you want to like block him
here is my modmail code
#client.event
async def on_message(message):
channel = client.get_channel(909853337510371449)
if message.author.id == client.user.id:
return
if message.author != message.author.bot:
if not message.guild:
await channel.send(f"[{message.author.mention}] {message.content}")
await client.process_commands(message)

How do i save discord reaction as a variable

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]

I want to send a DM through a discord bot to the author of the message recieved in "on_message" function. How can i do that?

from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commans.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
author = message.author
await message.channel.send(message.content)
How can i change the code in line 15 to send the message to "author"?
You can use Member.create_dm() for this. You can modify your code as follows:
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
c = await message.author.create_dm()
await c.send(message.content)
You also misspelt commands in bot = commands.Bot(... :)
You could use await ctx.author.send() so that whoever runs the command would get it sent in there dms
from discord.ext import commands
load_dotenv("token.env")
token = os.getenv("Token")
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("!"):
await bot.process_commands(message)
return
else:
author = message.author
await ctx.author.send(message.content)

On message delete message Discord.py

import discord
import asyncio
client = discord.Client()
#client.event
async def on_ready():
print ("I’m Now Online")
#client.event
async def on_message(message):
if message.author == client.user:
return
elif message.content.startswith("deletethis"):
I’m wonder how I could be able to add to this to delete the above command when the author of the message sends the command above.
May someone help create one? I’ve tried my self by brain and didn’t find anything online so I’m looking for some help maybe.
for async just do
await client.delete_message(message)
otherwise for rewrite just do
await message.delete()
Finished code:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("deletethis"):
await asyncio.sleep(1)
await message.delete()

Categories