A presence/activity set command?

2024/11/19 13:33:52

So, I was wondering if there could be a command I could write that allows me to set the bots presence and activity (ex. ~~set presence idle or ~~set activity watching "people typing ~~help") or something like that.

Unrelated question: How do I set commands to be used by me only?

I haven't found any example code for this, and i'm a beginner.

Answer

You can use the is_owner check to ensure that you are the only person who can invoke a command.

To change the presence or status of the bot, use the change_presence method:

from discord.ext.commands import Bot, is_owner
from discord import Status, Activity, ActivityTypebot = Bot("~~")def getEnum(enum):def getter(arg):return enum[arg]return getter@bot.group(invoke_without_command=True)
@is_owner()
async def set(ctx):await ctx.send("You must provide a subcommand.")@set.command()
async def presence(ctx, status: getEnum(Status)):await bot.change_presence(status=status)@set.command(invoke_without_command=True)
async def activity(ctx, type: getEnum(ActivityType), *, description):await bot.change_presence(activity=Activity(type=type, name=description))@set.error
async def set_error(ctx, error):if isinstance(error, BadArgument):await ctx.send(error.message)await ctx.send(error.args)bot.run("token")

The above will fail silently if you try to provide an unrecognized name to Status or ActivityType, you could also try writing an error handler to provide some feedback.

https://en.xdnf.cn/q/119946.html

Related Q&A

Inverted Index in Python not returning desired results

Im having trouble returning proper results for an inverted index in python. Im trying to load a list of strings in the variable strlist and then with my Inverse index looping over the strings to return…

Remove white space from an image using python

There are multiple images that have white spaces that I need to remove. Simply crop the image so as to get rid of the white spaces Heres the code I tried so far (this is a result of search) import nump…

How to calculate the ratio between two numbers in python

I have to calculate the ratio between 0.000857179311146189 and 0.026955533883055983 but am unsure how to do this other than by dividing the two numbers. Is it possible to calculate this with the result…

Built-in variable to get current function

I have a lot of functions like the following, which recursively call themselves to get one or many returns depending on the type of the argument: def get_data_sensor(self, sensorname):if isinstance(sen…

Python run from subdirectory

I have the following file hierarchy structure:main.py Main/A/a.pyb.pyc.pyB/a.pyb.pyc.pyC/a.pyb.pyc.pyFrom main.py I would like to execute any of the scripts in any of the subfolders. The user will pass…

How to create duplicate for each value in a python list given the number of dups I want?

I have this list: a=[7086, 4914, 1321, 1887, 7060]. Now, I want to create duplicate of each value n-times. Such as: n=2a=[7086,7086,4914,4914,1321,1321,7060,7060]How would I do this best? I tried a lo…

How do I generate random float and round it to 1 decimal place

How would I go about generating a random float and then rounding that float to the nearest decimal point in Python 3.4?

Error extracting text from website: AttributeError NoneType object has no attribute get_text

I am scraping this website and get "title" and "category" as text using .get_text().strip().I have a problem using the same approach for extracting the "author" as text.d…

Fastest way to extract tar files using Python

I have to extract hundreds of tar.bz files each with size of 5GB. So tried the following code:import tarfile from multiprocessing import Poolfiles = glob.glob(D:\\*.tar.bz) ##All my files are in D for …

Python - Split a string but keep contiguous uppercase letters [duplicate]

This question already has answers here:Splitting on group of capital letters in python(3 answers)Closed 3 years ago.I would like to split strings to separate words by capital letters, but if it contain…