How to Set What a Discord Bot Is Playing
Tutorial | Python | Discord.py
How to Update Discord bot status with Discord.py
Personalizing your bot's displayed activeness for your discord server with Python
Looking at the latest iteration of our Discord bot, it isn't very exciting. It sits online and watches. Let's update our bot's status when it comes online to brand it a bit more lively. If you missed the last postal service on how to use dotenv
to hide your token, see the link below.
The client
object for the bot has a method change_presence
. This method is used to change the bot'south status. In that location are a couple helper functions that nosotros tin can use to build statuses.
- Playing: Use
discord.Game()
to brandish the bot equally playing a game. Provide the proper noun of the game to theproper name
statement - Streaming: Apply
discord.Streaming()
to display the bot as streaming with an optionalurl
argument - Listening: Apply
discord.Activity()
with thetype
argument set up todiscord.ActivityType.listenting
to testify the bot as listening to something - Watching: Utilize
discord.Activity()
with thetype
argument set up todiscord.ActivityType.watching
to bear witness the bot as watching something
Examples
Setting the bot's condition when it showtime comes online will add a bit of polish while it is hanging out in the server. In order to keep things simple, we'll append the examples to the on_ready
upshot.
Let'southward showtime with ane of the nigh common statuses seen on Discord, playing a game. This is particularly relevant to the bot I originally fix out to build. The bot was going to exist in a server dedicated to Sea of Thieves. Naturally, I wanted the bot to exist playing Sea of Thieves around the clock.
Adding the following code in the on_ready
part sets the bot's status to "Playing Sea of Thieves".
# Startup Data
@client.issue
async def on_ready():
await client.change_presence(activity=discord.Game('Bounding main of Thieves')) print('Connected to bot: {}'.format(client.user.name))
print('Bot ID: {}'.format(customer.user.id))
Streaming Status
People dear to share their gameplay, and with the current pandemic conditions, it isn't as safe to hangout and play together in person. In the spirit of playing games together, let'south change the bot status to "Streaming Sea of Thieves" with a link to a Twitch stream.
Annotation: Discord only supports streaming links for YouTube and Twitch
With the streaming status, in that location are 2 arguments: name
and url
. The proper noun
argument is the game the bot will be "streaming." The url
argument will become a clickable link to picket the stream.
Note: the
url
statement must be formatted as an actual URL.http://www.twitch.television
will piece of work whilewww.twitch.tv
will not. It won't change the bot status at all
# Startup Information
@customer.event
async def on_ready():
await client.change_presence(activity=discord.Streaming(name='Ocean of Thieves', url='https://world wide web.twitch.tv/your_channel_here')) print('Connected to bot: {}'.format(customer.user.name))
print('Bot ID: {}'.format(customer.user.id))
Watching Condition
Afterwards a long mean solar day of managing servers, maybe the bot but wants to unwind with the latest episode of The Boys on Amazon Prime. To become the watching status, we have to switch the code up a piddling bit. Playing and streaming are so common that they go their ain elementary methods. Watching requires that we utilise discord.Activity
and define the activity with the type
argument fix to discord.ActivityType.watching
.
# Startup Information
@client.event
async def on_ready():
await customer.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, proper name='The Boys')) print('Connected to bot: {}'.format(client.user.proper noun))
impress('Bot ID: {}'.format(client.user.id))
Listening Condition
Listening functions the same manner as watching, just this time set blazon to discord.ActivityType.listening. At present it can listen to some custom beats!
# Startup Information
@client.outcome
async def on_ready():
await customer.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name='Some Custom Beats')) print('Connected to bot: {}'.format(client.user.proper noun))
impress('Bot ID: {}'.format(client.user.id))
Irresolute Condition for Long Running Actions
Sometimes commands run long and you may desire to update the condition for while the bot is doing a calculation and return to the previous status when it has completed. Allow's break down the problem into steps:
- Capture the bot's electric current activeness
- Alter the bot'southward activity to something new
- Simulate a long running task
- Change the bot's activeness back to the original action
Permit's first by capturing the bot's electric current activity. Our bot is named client
in the code, and then we can use that to save the current action to a variable.
previous_status = client.guilds[0].get_member(customer.user.id).action
Nosotros are accessing the beginning guild the bot is a member of, and so using the get_member
method with the bot ID to think its action
aspect.
Next, let's modify the bot activeness to the new activity. Since the bot is executing a command, permit'southward use the listening
action.
await client.change_presence(activity=discord.Activity(blazon=discord.ActivityType.listening, name='to Your Commands!'))
Now that the status has been updated, allow'due south do the long running part of the code. Nosotros'll simulate it hither using the time
module. Import fourth dimension
at the summit of your bot script.
import time
Now in our control, we tin can add a 5 second pause to the code to simulate the script doing some long running calculations.
time.sleep(v)
Finally, let's render the condition of the bot to whatever information technology was previously. To do this we simply pass the original condition to the change_presence
method. Since the activity is saved as the correct object type, there is no extra work to perform. The bot is at present back to its original activity.
await client.change_presence(activity=previous_status)
Altogether this would look something similar the longcommand
command defined beneath.
@client.command()
async def longcommand(ctx):
# Get previous activity
previous_status = client.guilds[0].get_member(client.user.id).activity # Change activity for the chore
await customer.change_presence(action=discord.Activity(type=discord.ActivityType.listening, name='to Your Commands!'))
# Long Running Chore
fourth dimension.slumber(5)
await ctx.ship('Chore Complete!')
# Reset the status
look client.change_presence(activity=previous_status)
Adding Dynamic Data to a Status
You may have seen bots in other servers that list information similar how many dissimilar servers they are a office of. client.guilds
is a list of the servers a bot has been added to (society is the developer proper name for servers). The length of this list is equal to the number of servers the bot is a part of. We can create a cord based off this data.
activity_string = 'on {} servers.'.format(len(client.guilds))
I like to remember my bot is always listening, then let'south use the listening command to display. This volition be but like our earlier apply of it, but now the proper noun
argument will be set to activity_string
.
# Startup Information
@client.event
async def on_ready():
expect customer.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, proper name=activity_string)) print('Connected to bot: {}'.format(customer.user.proper name))
print('Bot ID: {}'.format(client.user.id))
At present when the bot starts up it will automatically show how many servers it is a part of. This just updates when the bot starts, and then for at present it won't update in real fourth dimension equally the bot is added to more servers.
Caveats
I major issue with frequently irresolute the bot status is that Discord only supports a unmarried status per server. In other words, if the bot is in many servers, its condition will change in every server anytime a status change is triggered. For this reason many bots don't alter their status based on commands.
Some other limitation of the Discord API is that it does non at this fourth dimension back up custom statuses. This means that you can't create a "Dreaming" activity for your bot to dream of electrical sheep.
Quick Reference Codes
Source: https://python.plainenglish.io/how-to-change-discord-bot-status-with-discord-py-39219c8fceea
Belum ada Komentar untuk "How to Set What a Discord Bot Is Playing"
Posting Komentar