How do I make my Discord bot change status every 10 seconds?
Edited for users on v12 which now uses bot instead of client
const activities = [
"with the &help command.",
"with the developers console.",
"with some code.",
"with JavaScript."
];
bot.on("ready", () => {
// run every 10 seconds
setInterval(() => {
// generate random number between 1 and list length.
const randomIndex = Math.floor(Math.random() * (activities.length - 1) + 1);
const newActivity = activities[randomIndex];
bot.user.setActivity(newActivity);
}, 10000);
});
How to make my discord bot status change every 10 seconds?
Fist you need to create a list of activities. I will call it Activities_List. Note you can call it anything you would like. You can also add as many as you would like.
const activities_list = [
"Activitie One",
"Activitie Two",
"Activitie Three",
];
Then we would create the timer that changes the activity ever 60 sec. This will also pick one of the activities we have set in the activities list and display it.
client.on('ready', () => {
setInterval(() => {
const index = Math.floor(Math.random() * (activities_list.length - 1) + 1);
client.user.setActivity(activities_list[index], {type: 'WATCHING'});
}, 60000);
});
You can always change the 60000 out for 10000 which is ten seconds.
Making a discord bot change playing status every 10 seconds
Threading and asyncio don't play nice together unfortunately. You need to jump through extra hoops to await coroutines inside threads. The simplest solution is to just not use threading.
What you are trying to do is wait a duration and then run a coroutine. This can be done with a background task (example)
async def status_task():
while True:
await test_bot.change_presence(...)
await asyncio.sleep(10)
await test_bot.change_presence(...)
await asyncio.sleep(10)
@test_bot.event
async def on_ready():
...
bot.loop.create_task(status_task())
You cannot use time.sleep() as this will block the execution of the bot. asyncio.sleep() though is a coroutine like everything else and as such is non-blocking.
Lastly, the @client.event
decorator should only be used on functions the bot recognises as events. Such as on_ready and on_message.
How can I make my Discord bot change its status every 10 seconds or so? (Including things like "Listening to", "Playing" and "Watching")
I guess you have a problem with the quotation in an array, try this:
let activityTypes = ['PLAYING','STREAMING','LISTENING','WATCHING']
let randomType = activityTypes[Math.floor((Math.random()*activityTypes.length))]
setInterval(async ()=>{
await client.user.setActivity('Your text here.', { type: randomType })
},10000)
But keep in mind, 10 seconds will probably hit the discord API rate limit.
How do I make my Discord bot change status auto
I pasted your code and it totally worked as expected, a little thing: The setInterval
function waits before executing, means in your case: it waits 10 seconds and only after that it sets the status the first time, so you might want to change your code a little bit so that it already sets a status when starting and not after the first interval is over:
const actvs = [
"with the &help command.",
"with the developers console",
"with some code",
"with JavaScript"
];
client.on('ready', () => {
client.user.setActivity(actvs[Math.floor(Math.random() * (actvs.length - 1) + 1)]);
setInterval(() => {
client.user.setActivity(actvs[Math.floor(Math.random() * (actvs.length - 1) + 1)]);
}, 10000);
});
Edit: My Discord.js version is v11.5.1, it is different in v12. Here is a link to the docs for v12
Discord.js Bot Status
Note: This code is for Discord.js V13.3.1
For a rotating status, as you would describe, you would do this by placing some code in the ready
event that changes the status after a set period of time. This will require the util
package which can be installed using:
- npm:
npm install util
- yarn:
yarn install util
Once you have the package installed, you will create a function called wait
. It's a simple way to wait without blocking the rest of the script.
const wait = require('util').promisify(setTimeout);
This function will serve our primary purpose, making the delay between switching the statuses. Then, we will use setInterval
to continuously loop between the statuses.
<Client>.on('ready', async () => {
// ...
setInterval(async () => {
client.user.setPresence({ activities: [{ name: `Status1`, type: `PLAYING` }] });
await wait(5000); // Wait 5 seconds
client.user.setPresence({ activities: [{ name: `Status2`, type: `PLAYING` }] });
await wait(5000); // Wait 5 seconds
});
});
As you can see, there are two line that repeat themselves. That is: <Client>.user.setPresence(...)
and await wait(5000)
. The wait function will block the status from updating itself too early. You can edit the amount of time set by converting the seconds to milliseconds (5 seconds becomes 5000). The other portion sets the bot's status. The type shows what the bot is doing. The valid values for this are found in the docs. You can simply copy and paste the two lines and add more statuses as needed.
Related Topics
Typeerror: Missing 1 Required Positional Argument: 'Self'
Reading Contents of a Gzip File from a Aws S3 in Python
Python: How to Calculate the Sum of Numbers from a File
Pandas Series With Different Lengths
In Python, How to Check If a Date Is Valid
Unit Testing a Method With No Return Value
Jupyter Notebook Python Nameerror
Maximum Characters That Can Be Stuffed into Raw_Input() in Python
How to Strip Comma in Python String
Using Opencv to Overlay Transparent Image Onto Another Image
Finding the Value of the Min and Max Pixel
How to Print Superscript in Python
Python3 Tkinter Set Image Size
Python-3: Why This Following Code Returns None in Print Statement
Combine Year, Month and Day in Python to Create a Date
Unpivot Multiple Columns With Same Name in Pandas Dataframe
How to Write a Python Script That Can Read Doc/Docx Files and Convert Them to Txt