Error in on_message when i try to add reaction python - python

I want to make bot react to every message in specific channel, this is my code:
#bot.event
async def on_message(message):
channel = bot.get_channel(837590298435649537)
if message in channel:
await message.add_reaction("✅")
And this is error:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 15, in on_message
if message in channel:
TypeError: argument of type 'TextChannel' is not iterable
I don't know what's wrong, please help

You can't use message in channel you would have to use
if message.channel == channel:
await message.add_reaction("✅")

Related

Python discord.py guild memberlist not work

I try
guild = client.get_guild()#input guild-id
async for member in client.fetch_members(limit=150):
print(member.name)
error code:
Ignoring exception in on_ready Traceback (most recent call last):
File "D:\studio\share\crawling\lib\site-packages\discord\client.py",
line 343, in _run_event
await coro(*args, **kwargs) File "D:\studio\onga\main.py", line 20, in on_ready
async for member in client.fetch_members(limit=150): AttributeError: 'Client' object has no attribute 'fetch_members'
There is already a simple way to get the member count the guild.member_count
#bot.command(aliases=["mc"])
async def membercount(ctx):
guild = ctx.guild
await ctx.reply(f"There is {guild.member_count} Members In The {guild.name}")```

Discord.py, send a message to a channel I just created

I've been trying to run a loop that creates a text channel and directly after that, sends multiple messages to it. I've come up with this code:
#client.event
async def on_message(message):
msg = message.content
guild = message.guild
if msg.startswith("test") and message.author.id == admin:
await message.reply("valid", mention_author=True)
for i in range (10):
await guild.create_text_channel('test channel')
for j in range (5):
await i.send("ah")
Unfortunately, it gives me this error:
Ignoring exception in on_message
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "main.py", line 29, in on_message
await i.send("ah")
AttributeError: 'int' object has no attribute 'send'
Could someone help me with this?
You're not using the result of the channel creation, instead you're using i which is your index in the iteration. As described in the documentation, create_text_channel returns a TextChannel object, try using that instead:
for i in range (10):
channel = await guild.create_text_channel('test channel')
for j in range (5):
await channel.send("ah")
i, in this case, is an integer, so that is why you can't do:
await i.send(<message>)
What you are supposed to do is:
await channel.send(<message>)

Add Role on Server

I intend, if you send the bot a certain DM that it then gives a role on a certain server.
This is my code what I tried:
#bot.event
async def on_message(message):
if message.author.bot:
return
elif message.content.startswith("++neunundvierzig++"):
role = message.author.guild.get_role(861543456908640298)
guild = bot.get_guild(816021180435529758)
await message.channel.send("Erfolgreich Zerifiziert!")
await member.add_roles(role)
It says in the log:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\aaa12\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "f:\HTML CSS JS Py Projekte\Python\bot_check\bot.py", line 36, in on_message
role = message.author.guild.get_role(861543456908640298)
AttributeError: 'User' object has no attribute 'guild'
I think you defined role wrong. Your code says that message.author.guild doesn't exist, so you should try message.guild.
Like this:
role = message.guild.get_role(861543456908640298)
Aside from that, you don't defined member. Just rename it to await message.author.add_roles(role) instead of await member.add_roles(role)
Sources
Discord Messages

How can I fix this discord sound Bot error?

Hey I started coding lately, now i want to do a Discord bot who can join a Voice Chanel. When i execute this:
BOT_PREFIX = '%'
bot = commands.Bot(command_prefix=BOT_PREFIX)
#bot.commmands(pass_context=True, aliases=['j', 'joi'])
async def join (ctx):
channel = ctx.message.author.voice.channel
voicechannel = await channel.connect()
after this Error shows up:
Traceback (most recent call last):
File "C:\DEV\Python\Einführung\venv\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "C:/DEV/Python/pythonProject1/venv/Discord Bot/Bot.py", line 23, in on_message
#bot.commmands(pass_context=True, aliases=['j', 'joi'])
AttributeError: 'Bot' object has no attribute 'commmands'
Its #bot.command() not #bot.commands. Commands cannot be used in the case here.
Also Can Do
channel = ctx.author.voice.channel Rather than ctx.message.author.voice.channel
and Passing a variable for await channel.connect() is not needed .

discord.py Fetching messages by content

I'm trying to build a discord bot for reaction roles. To do this, I'm trying to use the on_reaction_add combined with fetch_message to check if the reaction added was to the message that the bot sent but I keep getting various error with fetch_message. Here is the code:
#bot.command()
async def createteams(ctx):
msg = await ctx.send("React to get into your teams")
await msg.add_reaction("1️⃣")
await msg.add_reaction("2️⃣")
await msg.add_reaction("3️⃣")
await msg.add_reaction("4️⃣")
await msg.add_reaction("5️⃣")
await msg.add_reaction("6️⃣")
await msg.add_reaction("7️⃣")
await msg.add_reaction("8️⃣")
#bot.event
async def on_reaction_add(reaction, user):
id = reaction.message.id
if await reaction.message.author.fetch_message(reaction.message.id) == "React to get into your teams":
print("Success")
This gives me the error
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "hypixel.py", line 60, in on_reaction_add
fetch = await reaction.message.author.fetch_message(id)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/abc.py", line 955, in fetch_message
channel = await self._get_channel()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/member.py", line 243, in _get_channel
ch = await self.create_dm()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/member.py", line 109, in general
return getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'
But when I go and copy the ID of the message I reacted to, its the same as the printed variable id but it still says Message Not Found
Thanks
I figured out the issue, you can't use fetch_message with user it needs to be channel so I changed my code to
await reaction.message.channel.fetch_message(id)
and it worked :)

Categories

Resources