Pycord message.content blank on guilds - python

I want to develop a discord bot. After trying the bot on my Raspberry Pi, I cannot read messages of the bot on the guilds. On the Raspberry Pi the program is working without any problems. Obviously I turned the pi off before starting the bot. I already tried kicking and reconnecting the bot on the servers, but that didn`t work. Private messages to the bot are running smoothly.
The messages are blank, so the event is called, but nothing is printed to the command line.
import discord
from discord.ext import commands
bot = commands.Bot()
#bot.event
async def on_message(message):
print(message.content)
bot.run("TOKEN")
The content of the message variable is (I have changed the ids and some other variables):
<Message id=953328581969116272 channel=<TextChannel id=956654537448771667 name='general' position=0 nsfw=False news=False category_id=953322537446663665> type=<MessageType.default: 0> author=<Member id=689200156665367842 name='my_name' discriminator='0000' bot=False nick=None guild=<Guild id=953322535556471664 name='Test' shard_id=0 chunked=False member_count=2>> flags=<MessageFlags value=0>>
I`ve found an open github problem, but because discord.py is not continued I have to ask it here. (The link: https://github.com/Rapptz/discord.py/issues/6820)

Switching back to API version 9 has solved the issue for me.
I've encountered the same issue and found the solution in the official Pycord Discord.
They have switched to the Discord API version 10 where message content has become a privileged intent in accordance with the new upcoming Discord changes (Message Content: Privileged Intent for Verified Bots).
In short, if your bot is in more than 75 servers you will need to verify it in order to access privileged intents.
Here's Pycord's detailed post for reference
Dated 20/03/2022
Hello #v2.0 Testers, Along with
https://github.com/Pycord-Development/pycord/pull/1012, pycord has
switched to API version 10. This version introduces a new privileged
intent: message content. Bots that use the master branch of pycord
need to turn this intent on in order to receive message contents and
invoke prefixed commands. However if you don't want to switch to v10,
you can change the API version back to 9
(https://github.com/Pycord-Development/pycord/pull/1032)
Turn the intent on:
bot = commands.Bot(..., intents=discord.Intents(message_content=True))
Switch back to API version 9:
discord.http.API_VERSION = 9
bot = commands.Bot(...)
See https://support-dev.discord.com/hc/en-us/articles/4404772028055
for more information about the intent.
This is the link to their Discord server. I would have not found the solution without being part of it.
https://discord.gg/sb5zZYnz

From October 25, 2021, accessing message content now requires enabling a new privilege intent.
Head to your developer portal and do this.
Applications ↦ Settings ↦ Bot ↦ Privileged Gateway Intents

Discord.py is back to development again, you should use it. You can ask these question in the discord.py Discord server and get quick response other than asking it at here!
Have fun coding!

Related

Discord.py Edit Channel when the Bot goes offline

Here my Code:
#bot.event
async def on_disconnect():
channel = bot.get_channel(1064939978569822309)
await channel.edit(name="Bot status: 🔴")
I dont have any errors but nothing happends if the bot disconnect.
Can anyone help me?
I reinstall the library.
According to discord.py's documentation. on_disconnect() is used for when the bot cannot connect to the discord server.
If the bot cannot connect to the server, it would not be able to get the discord channel (think about it, if you can't connect to discord you would not be able to send any message at all, much less editing the channel).
on_disconnect() is probably more for the usage on the local side of things (e.g. printing out logs to check why the bot cannot connect to the discord server) rather than to update the bot's status.
It is not possible to do this automatically, unless you have another bot monitoring this bot (which is not really viable as both bots could be down at the same time).
If you want to handle cases where you have to shutdown the bot (for maintenance purposes, as an example), you can instead have a function (an idea is you can have it as a discord command that can only be run by the bot's owner) to edit the channel before going offline.

Check if Discord bot has slash command permissions in server?

I run a bot that is in several thousand Discord servers - until now, its invite link has not included the application.commands scope, only bot.
I understand that if I want to transition towards Slash Commands, the bot's users will need to re-invite it with the new permissions.
I plan to keep the traditional method of using bot commands for as long as possible, but is it possible to determine whether my bot has application.commands permissions whenever a user from a server interacts with it?
My main reason for wanting to do this is so that I can leave a message in the server encouraging them to re-invite the bot so that slash commands can be used.
I'm using discord.py and discord-py-slash-command.
When using discord.py 2.0 or any of the many forks, you can do the following:
from discord.bot import ApplicationCommandMixin
bot = commands.Bot()
for guild in bot.guilds:
try:
await ApplicationCommandMixin.get_desynced_commands(bot, guild.id)
except discord.errors.Forbidden:
print(f"Slash commands are disabled in {guild}")
What happens is that it tries to check if a guild's application commands are out of sync. However, if the bot does not have the application.commands scope, an error will be raised. By catching the error, we can check if the bot has the scope or not.
you can DM the server owner by using code below
owner = guild.owner
await owner.send("hi there!")
Run the code and it should DM the owner.
Alternatively you can mention in your commands to switch and use to slash cmds like arcane bot has done

Discord bot not showing up in a server

I've created a bot and added to a server that I created for the purpose of bot testing as well as a server that I have admin permissions in. In the server I created, I can see that the bot is online and a member and can call the commands with no issue. However, I cannot see the bot in the other server and cannot call the commands.
I followed the same procedure for adding the bot to both servers by going thru the OAuth2 api.
I know that the bot connected to both servers because when I run:
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
print(bot.guilds)
I see both servers in the output.
Not really sure what the issue here is. If anyone can shed some light on potential causes of this issue. I'd greatly appreciate it. Thanks!
EDIT: I also see the bot in the 'Integrations' tab of 'Server Settings' for both servers.

Is it possible to harvest messages from other peoples discord servers?

Can I use the discord API in python to harvest messages from a server (not my own server)? Assuming you have an invite link.
Thanks
Yes, it would be possible. If you look under the TextChannel section of the Discord Models section of the discord.py API Reference, there is the TextChannel history() method. This can be used to parse all the messages in a channel. If the limit argument of the method is set to None, then all the messages in the channel will be returned.
Since you say that the bot already has an invite, that implies they have access to a good part of the server. The method I mentioned requires the following permissions for your bot:
See Text Channels
Read Message History
Well, if your using a discord bot, you need them to invite your bot to their server.
Other than that you could theoretically listen with a bot on your own account but that would be against the discord TOS.
You can achieve this through a discord bot. Given that bots respond to events that fire within servers they reside in, you can listen in to incoming messages.
An example of such event can be found in discord-py documentation.

Discord Bot clear command doesn't execute after I added it to other server | Python

I'm clearly new to making bot on discord, might aswell python language. I just made a simple local bot which does have a clear command. At first it's working on my server but after when I added it to my friend's server it doesn't clear the amount of messages. I don't know if it's bug but I have the codes here, references just came from other youtubers.
#bot.command()
async def clear(ctx, amount=10):
await ctx.channel.purge(limit=amount)
the code is that simple but it doesn't execute in the server. Idk what happened to it.
Edit:
I just realised there are some errors that has happened while executing that command. The command was being ignored
Ignoring exception in command clear:
When, as an owner, you invite a bot to your server, you do it via a webpage. The webpage is displaying all the permissions that the bot asks and you can decide to grant it these permissions or not (by unchecking checkboxes).
Your friend might have unchecked some checkboxes when he invited your bot on its server, thus not granting it the permissions it needs to delete messages.

Categories