Reputation: 6558
I've created a simple bot that sends a message to every text channel on my own server at if the time equals midnight.
The idea is to prune the Rhythm bot's message history without having to do it myself:
// Run dotenv
require('dotenv').config();
// Import libraries
const Discord = require('discord.js');
const client = new Discord.Client();
// Event listener when a user connected to the server.
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
setInterval(function()
{
var date = new Date();
if (date.getHours() == 1) {
if (client.channels) {
client.channels.cache.forEach(function(el)
{
if (el.type === 'text') {
client.channels.cache.get(el.id).send('-prune');
}
});
}
}
}, 1000);
// Initialize bot by connecting to the server
client.login(process.env.DISCORD_TOKEN);
currently, it outputs the message -prune
to each channel successfully, however, the rhythm bot/discord server seems to ignore the message as a command and instead, treats it as plain text. When I type in -prune
, it instantly works.
I even did the unthinkable and set the scope to bot and permissions to admin on the Discord Developer site, so how do I go about getting my bot to prune the messages so I don't have to?
Upvotes: 0
Views: 1849
Reputation: 4520
As aforementioned, you generally cannot run other bots' commands using your own. All decently developed bots specifically check if the author of a message is a bot, and prevent processing the message if that is the case. This is to prevent spam, avoid abuse, and prevent accidental misinterpretation of bots' messages as commands.
Instead of using Rythm's prune command, you simply need to execute the same functionality yourself. One way to do this would be to loop through all of your channels, fetch as many messages sent by Rythm as possible, and bulk delete them. That solution has already been demonstrated by the other answer.
Another way to go about this is to setup a specific channel solely for Rythm commands (like a #music
text channel). Then, instead of sending -prune
, your bot could simply clone and delete the #music
channel at midnight, effectively clearing all Rythm commands. Plus, this comes with the added bonus of increased organization and simplicity. Here is a simple example of somewhat similar functionality from one of my bots; it gets the channel, clones it, sets the clone's position in the channel list, and then deletes the original channel.
Here's an untested example of how this might look in your code:
setInterval(async function()
{
var date = new Date();
if (date.getHours() == 1) {
if (client.channels) {
//Gets the `#music` channel, if it exists in the cache
let channel = client.channels.cache.find(ch => ch.name == "music");
if (!channel) return;
//Gets the position of the music channel in the channel list
let pos = channel.position;
//Clones the music channel, and sets its position to `pos`
let newChannel = await channel.clone();
await newChannel.setPosition(pos);
//Deletes the original channel
channel.delete();
}
}
}, 1000);
Note that this is not actually "clearing" any messages or the channel. It is actually fully deleting the original channel, after creating a new one; doing so, however, creates the illusion of the channel being cleared. The new channel is a clone, and retains all of the permissions and settings of the original. In addition, unless you specifically add code to retain pinned messages, you will lose pinned messages in the original channel.
Also note that the above code snippet is just an example; you will want to add error handling and such to it (for example, to prevent deleting the original channel if the cloning process failed).
If you need to delete over 100 Rythm command messages in a single day, and are willing to condense all Rythm commands to 1-2 channels, this solution will be much simpler than the bulk-deletion method for over 100 messages. If not, which method you want to use is entirely up to you. Rythm can be configured to only allow sending commands in specified channels, which could be of use to you.
Upvotes: 0
Reputation: 9041
Discord bots aren’t supposed to be able to get commands from other bots. This is to prevent spam. Another way is self-botting, but that is strictly against TOS. The best way you could code it yourself by using this instead:
client.channels.cache.forEach(async chan => {
let messages = await chan.messages.fetch({limit: 100})
messages = messages.filter(m => m.author.id === 'rythm bot id')
chan.bulkDelete(messages)
})
This is untested code and you will have to add your own error handling. This only deletes Rythm's messages if they are within 100 messages from the bottom.
Upvotes: 0