so i made a code that if a person sends an attachment ina certain channel it would react to it but I can't seem to make it work
Here's my code
#bot.event
async def on_message(message):
if message.attachments in message.channel.name == "memes-only":
await asyncio.sleep(2)
await message.add_reaction("😭")
await message.add_reaction("🥲")
channel = message.channel
await channel.send("i have reacted")
await message.delete()
i have also tried it with if message.attachments in message.channel.id == "860073616606625792":
edit: my traceback says TypeError: argument type of int is not iterable
Okay, so you have this weird if statement
if message.attachments in message.channel.name == "memes-only":
the thing it is doing is first checking is message.attackments is in message.channel.name == 'memes-only' which is a boolean value
what you want to do is
#bot.event
async def on_message(message):
if message.channel.name == "memes-only":
await asyncio.sleep(2)
await message.add_reaction("😭")
await message.add_reaction("🥲")
channel = message.channel
await channel.send("i have reacted")
await message.delete()
Related
So basically I have this problem, any command that I type does not work. There are no errors, and everything else is working fine. It's just that for some reasons #bot.command() isn't working, and that is kind of annoying.
import discord
from discord.utils import get
from discord.ext import commands
import time
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix = '$', intents=intents)
TOKEN = 'hi'
ROLE = 'hi'
db1 = [hi, hi]
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
await bot.process_commands(arg)
#bot.event
async def on_member_join(member):
role = get(member.guild.roles, name=ROLE)
await member.add_roles(role)
await member.send('hi')
try:
channel = member.guild.system_channel
embedVar = discord.Embed(title="Welcome <#{}> in {} ".format(str(member.id),str(member.guild)), description="hi", color=0x00ff00)
await channel.send(embed=embedVar)
except:
channel = member.guild.get_channel(hi)
embedVar = discord.Embed(title="Welcome <#{}> in {} ".format(str(member.id),str(member.guild)), description="hi", color=0x00ff00)
await channel.send(embed=embedVar)
#bot.event
async def on_member_remove(member):
try:
channel = member.guild.system_channel
embedVar = discord.Embed(title="Bye {} from {} ".format(str(member.name),str(member.guild)), description="Come back when you want", color=0x00ff00)
await channel.send(embed=embedVar)
except:
channel = member.guild.get_channel(hi)
embedVar = discord.Embed(title="Bye {} from {} ".format(str(member.name),str(member.guild)), description="Come back when you want", color=0x00ff00)
await channel.send(embed=embedVar)
#bot.event
async def on_invite_create(invite):
channel = bot.get_channel(hi)
await channel.send("An invite has been created, {}, by <#{}> on {}".format(str(invite.url), str(invite.inviter.id), str(invite.created_at)))
#bot.event
async def on_invite_delete(invite):
channel = bot.get_channel(hi)
await channel.send("An invite has been deleted by{}".format(str(invite.inviter.id)))
#bot.event
async def on_member_ban(guild, member):
channel = bot.get_channel(hi)
embedVar = discord.Embed(title="Ban", description="Ban requested on<#{}>".format(str(member.id)))
await channel.send(embed=embedVar)
#bot.event
async def on_member_unban(guild, member):
channel = bot.get_channel(hi)
embedVar = discord.Embed(title="Unban", description="Unban requested on<#{}>".format(str(member.id)))
await channel.send(embed=embedVar)
#bot.event
async def on_ready():
print(f'{bot.user} succesfully logged in')
return
#bot.event
async def on_message(message):
if message.content.startswith('purge requested by'):
time.sleep(1)
await message.delete()
if message.author == bot:
return
if message.content == 'hi':
await message.channel.send('hi')
if message.content.startswith('binvites'):
totalInvites = 0
for i in await message.guild.invites():
if i.inviter == message.author:
totalInvites += i.uses
await message.channel.send(f"You have invited {totalInvites} member{'' if totalInvites == 1 else 's'} to the Central Trade server")
if message.content == 'bpurge':
if message.author.id in db1:
await message.channel.purge(limit=10)
await message.channel.send('purge requested by <#{}>'.format(str(message.author.id)))
else:
return
if message.content == 'block':
if message.author.id in db1:
channel = message.channel
overwrite = channel.overwrites_for(message.guild.default_role)
overwrite.send_messages = False
await channel.set_permissions(message.guild.default_role, overwrite=overwrite)
embedVar = discord.Embed(title="Lock", description="Channel lock request by <#{}>".format(str(message.author.id)), color= 0x00FFFF)
await message.channel.send(embed=embedVar)
else:
await message.author.send('You do not have the permission to use this command')
if message.content == 'bunlock':
if message.author.id in db1:
channel = message.channel
overwrite = channel.overwrites_for(message.guild.default_role)
overwrite.send_messages = True
await channel.set_permissions(message.guild.default_role, overwrite=overwrite)
embedVar = discord.Embed(title="Unlock", description="Channel unlock request by <#{}>".format(str(message.author.id)), color=0xC0C0C0)
await message.channel.send(embed=embedVar)
else:
await message.author.send('You do not have the permission to use this command')
if message.content == 'test':
embedVar = discord.Embed(title="test", description="test", color=0x00ff00)
embedVar.add_field(name="test", value="test", inline=False)
embedVar.add_field(name="test", value="test", inline=False)
await message.channel.send(embed=embedVar)
if message.content == 'bpurges':
if message.author.id in db1:
await message.channel.purge(limit=10000)
await message.channel.send('purge requested by <#{}>'.format(str(message.author.id)))
embedVar = discord.Embed(title="Purge", description="Purge requested by<#{}>".format(str(message.author.id)))
await message.channel.send(embed=embedVar)
time.sleep(1)
await message.channel.delete()
bot.run(TOKEN)
Anyone has an idea why it's not working ? Also seems like i need to post more details, don't pay attention to this : Roméo et Juliette (Romeo and Juliet) est une tragédie de William Shakespeare.
#bot.event
async def opt1(msg):
await msg.channel.purge(limit=100, check= lambda x: ('++' in x.content.strip().lower()) and x.channel.id == 954573387387265024)
#bot.event
async def on_message(message):
if '++' in message.content and message.channel.id == 954573376209420328:
await opt1(message)
What I want to do is if a user types ++ in a channel 954573376209420328 then all the messages that contain ++ in a channel 954573387387265024 will be deleted by a bot.
You almost got it. You just have to delete #bot.event from opt1 function.
async def opt1(msg):
await msg.channel.purge(limit=100, check= lambda x: ('++' in x.content.strip().lower()) and x.channel.id == 954573387387265024)
#bot.event
async def on_message(message):
if '++' in message.content and message.channel.id == 954573376209420328:
await opt1(message)
async def on_message(message):
purgeChannel = bot.get_channel(954573387387265024)
if '++' in message.content and message.channel.id == 954573376209420328:
await purgeChannel.purge(limit=100, check = lambda x: ('++' in x.content.strip().lower() and x.author.id == message.author.id))
Okay I figured out it by myself, it was much simpler than I thought
I want to create a ChangeLog room where updates are displayed. The on_message part works without problem.
Screen of the message displayed in the salon
And the on_raw_reaction_add part does not work. There are no errors in the console. The user would normally have an additional role (ChangeLog) when clicking on the bell emoji.
Screen Role name
My code:
#bot.event
async def on_message(message):
await bot.process_commands(message)
if (message.channel.id == 846081235465797662):
if message.author.bot:
return
else:
await message.delete()
channel = bot.get_channel(846081235465797662)
desc = await message.channel.send(
f"**⚙️ CHANGELOG DU {ddmmYY} - <#&851702594837413929>**\n\n" +
message.content)
await desc.add_reaction('🔔')
#bot.event
async def on_raw_reaction_add(payload):
emoji = payload.emoji.name
canal = payload.channel_id
message = payload.message_id
role = discord.utils.get(bot.get_guild(payload.guild_id).roles,
name="ChangeLog")
membre = bot.get_guild(payload.guild_id).get_member(payload.user_id)
if canal == 846861087600148540 and emoji == "🔔":
await membre.add_roles(role)
incorrect code:
#bot.event
async def on_raw_reaction_add(payload):
emoji = payload.emoji.name
...
if canal == 846861087600148540 and emoji == "🔔":
await membre.add_roles(role)
payload.emoji.name == "🔔"?
correct code:
#bot.event
async def on_raw_reaction_add(payload):
emoji = payload.emoji.__str__()
...
try it
I am making a discord bot with rewrite, when the command runs, the event must finish, but if I want to execute another command I can't beacase the previous one it's not finished and It will send the other messages, how can I stop that?
#client.event
async def on_message(message):
def check(m):
return m.channel == message.channel and m.author != client.user
if message.content.startswith("!order"):
channel = message.author
await channel.send("in game name")
in_game_name = await client.wait_for('message', check=check)
await channel.send("in game ID")
in_game_ID = await client.wait_for('message', check=check)
await channel.send("cargo type")
cargo_type = await client.wait_for('message', check=check)
await channel.send("cargo limit")
cargo_limit = await client.wait_for('message', check=check)
await channel.send("storage")
storage = await client.wait_for('message', check=check)
await channel.send("priority")
priority = await client.wait_for('message', check=check)
You could raise an exception in your check if it sees a certain word. Here, if the bot sees the message CANCEL it will cancel the command:
#client.event
async def on_message(message):
def check(m):
if m.channel == message.channel and m.content == "CANCEL":
raise ValueError("Cancelled command")
return m.channel == message.channel and m.author != client.user
if message.content.startswith("!order"):
channel = message.author
await channel.send("in game name")
in_game_name = await client.wait_for('message', check=check)
I coded a Discord self bot that sends a picture that acts like a global emote without nitro.
Example:
me: !ok
me(bot): ok.png
But when I edit my code and add more pictures and stuff and I load it again (By doing "mybot.py" in cmd) it takes around 5 minutes to finally print "Self bot working." in the cmd window... what is the problem? I don't think it's a problem with my code because it's working overall. I'm using python 3.6 and windows.
import discord
import asyncio
client = discord.Client()
#client.event
async def on_ready():
print("nonitro Loaded.\n")
#client.event
async def on_message(message):
if message.author == client.user:
if(message.content.startswith("!ok")):
emo = "./img/ok.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
if message.author == client.user:
if(message.content.startswith("!xd")):
emo = "./img/xd.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
if message.author == client.user:
if(message.content.startswith("!coffee")):
emo = "./img/coffee.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
if message.author == client.user:
if(message.content.startswith("!thonk")):
emo = "./img/thonk.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
if message.author == client.user:
if(message.content.startswith("!like")):
emo = "./img/like.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
if message.author == client.user:
if(message.content.startswith("!kek")):
emo = "./img/kek.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
if message.author == client.user:
if(message.content.startswith("!bobross")):
emo = "./img/bobross.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
if message.author == client.user:
if(message.content.startswith("!likethis")):
emo = "./img/likethis.png"
async for msg in client.logs_from(message.channel,limit=1):
await client.delete_message(msg)
await client.send_file(message.channel, emo)
client.run("my token", bot=False)
You don't need to pull logs from the chat in order to delete that one message, you can just do
#client.event
async def on_message(message):
if message.author.id == "your id" and "!ok" == message.content:
await client.delete_message(message)
await client.send_file(message.channel,"path/to/file")
return
client.run("token",bot=False)
Which will send message only if the content of the message is !ok and if its your id thats saying !ok