Reputation: 25
I'm trying to make a bot that will write to the chat what the user is playing, but even when the game is running, None is displayed all the time
What am I doing wrong?
Working code:
from discord.ext import tasks
import discord
intents = discord.Intents.all()
intents.presences = True
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
@tasks.loop(seconds=5)
async def activity_task(self, message):
mentions = message.mentions
if len(mentions) == 0:
await message.reply("Remember to give someone to get status!")
else:
activ = mentions[0].activity
if activ is None:
await message.reply("None")
else:
await message.reply(activ.name)
@activity_task.before_loop
async def before_my_task(self):
await self.wait_until_ready()
async def on_message(self, message):
if message.content.startswith('!status'):
self.activity_task.start(message)
client = MyClient(intents=intents)
client.run('token')
Upvotes: 1
Views: 5139
Reputation: 344
As Ceres said, you need to allow intents.
Go to your developer's page https://discord.com/developers/applications, and go to the bot. Scroll down a bit, and you'll see this:
Turn on presence and server members intent
.
Now, in your code, you'll have to add this in the beginning:
intents = discord.Intents.all()
Change your bot startup code to this
client = MyClient(intents=intents)
Now, with the intents, you want the OTHER person's activity.
So, in the activity_task
method, you can't use message.author
, as that will return whoever sent the message, not who you're mentioning.
Change it to this:
async def activity_task(self, message):
mentions = message.mentions
if len(mentions) == 0:
await message.reply("Remember to give someone to get status!")
else:
activ = mentions[0].activity
if activ == None:
await messag.reply("None")
else:
await message.reply(activ.name)
Now, if you do !status @[valid ping here]
, it should return whatever they're doing. Must note: it MUST be a valid ping.
Upvotes: 4