Skip to content

Instantly share code, notes, and snippets.

@Skarlett
Last active March 26, 2024 10:30
Show Gist options
  • Save Skarlett/1c9a746ccf668c2bd30cfc0b4903127b to your computer and use it in GitHub Desktop.
Save Skarlett/1c9a746ccf668c2bd30cfc0b4903127b to your computer and use it in GitHub Desktop.
rust self bot using serenity
/*
** Self bot usage for serenity.
** Avoid API calls which handle
** guild invitation or require guild intent
**
** These tend to be where the alarms are set.
** Avoid prefixed commands & instant replies.
**
*/
use serenity::prelude::*;
use serenity::async_trait;
use serenity::model::Message;
struct EvHandler;
impl EventHandler for EvHandler {
/* on_ready doesn't work. */
/* on_message */
async fn message(&self, ctx: Context, ev: Message)
{
println!("{}:{}", message.author.name, message.content);
if ev.content.starts_with("!AAAA") {
ev.channel_id.say(&ctx.http, "AAAA!").await;
}
}
}
#[tokio::main]
async fn main() {
/*
** Fill in your user token.
** obtain via js: console.log(w.localStorage.token)
** Or search for "Authenication" HTTP header
*/
let user_token = "";
let self_bot = Client::builder(user_token)
.event_handler(EvHandler)
.await
.expect("Error creating client");
if let Err(why) = self_bot.start().await {
eprintln!("Error: {:?}", why);
}
}
@jodonnell77
Copy link

While trying to make a CLI client. When I used this example code, I could only see DMs, I could not see messages in server channels. The message content would be blank. I cannot figure out how to get around intents with serenity.

@Bwine127
Copy link

Bwine127 commented Mar 21, 2024

I see error when sending message using user token:
Error sending message: Http(UnsuccessfulRequest(ErrorResponse { status_code: 401, url: "https://discord.com/api/v10/channels/ Channel ID /messages", method: POST, error: DiscordJsonError { code: 0, message: "401: Unauthorized", errors: [] } }))
I don't know is it related but, on discord client, they use v9 API

@Skarlett
Copy link
Author

Skarlett commented Mar 21, 2024

Shortly after writing this, this method has stopped working.

I now recommend running vencord. @Bwine127

@Skarlett
Copy link
Author

It is also worth noting that discord does allow for self bots, but registered under this.

https://discord.com/developers/docs/change-log#userinstallable-apps-preview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment