Skip to content

Commit

Permalink
fix: use self bot fork
Browse files Browse the repository at this point in the history
oSumAtrIX committed Jul 9, 2023
1 parent bc15ed5 commit b77f9ff
Showing 5 changed files with 37 additions and 39 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DISCORD_TOKEN=YOUR_TOKEN_HERE
SELF_BOT_USER_ID=YOUR_ID_HERE
OWNER_ID=YOUR_ID_HERE
11 changes: 4 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ edition = "2021"
authors = ["oSumAtrIX <johan.melkonyan1@web.de>"]

[dependencies]
poise = "0.5.5"
poise = { git = "https://github.com/Sideral-Tech/poise" }
tracing = "0.1.23"
tracing-subscriber = "0.3.16"
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
33 changes: 14 additions & 19 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use poise::serenity_prelude::{CacheHttp, ChannelId, GuildId};
use crate::{Context, Error, CLYDE_ID};

/// Show this help menu.
#[poise::command(prefix_command, track_edits, slash_command)]
#[poise::command(prefix_command)]
pub async fn help(
ctx: Context<'_>,
#[description = "Specific command to show help about"]
@@ -14,7 +14,7 @@ pub async fn help(
ctx,
command.as_deref(),
poise::builtins::HelpConfiguration {
extra_text_at_bottom: "This is an example bot made to showcase features of my custom Discord bot framework",
extra_text_at_bottom: "Proxy Clyde from one server to another",
..Default::default()
},
)
@@ -29,51 +29,49 @@ pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
Ok(())
}

/// Pair Clyde from another Discord server with this server.
#[poise::command(slash_command, owners_only)]
pub async fn pair(
/// Proxy Clyde from another Discord server to your server.
#[poise::command(prefix_command, owners_only)]
pub async fn proxy(
ctx: Context<'_>,
#[description = "The guild ID of the other server"] guild_id: String,
#[description = "The channel ID of the other server"] channel_id: String,
) -> Result<(), Error> {
let guild_id = GuildId(guild_id.parse()?);
let channel_id = ChannelId(channel_id.parse()?);
let to_channel_id = ctx.channel_id();

let cache = ctx.cache().unwrap();
let http = ctx.http();

let Some(guild) = cache.guild(guild_id) else {
let Ok(guild) = http.get_guild(guild_id.0).await else {
return Err("Guild not found".into());
};

if cache.channel(channel_id.0).is_none() {
if http.get_channel(channel_id.0).await.is_err() {
return Err("Channel not found".into());
};

{
let mut config = ctx.data().pair_config.lock().await;
config.guild_id = guild_id;
config.from_channel_id = channel_id;
config.to_channel_id = to_channel_id;
}

ctx.defer_ephemeral().await?;

ctx.say(format!(
"Proxying <@{}> in channel <#{}> from guild {} to channel {}.",
CLYDE_ID, channel_id, guild.name, to_channel_id.0
"Proxying <@{}> in channel <#{}> from guild \"{}\".",
CLYDE_ID, channel_id, guild.name
))
.await?;
Ok(())
}

/// Send a message to the paired server.
#[poise::command(slash_command, owners_only)]
#[poise::command(prefix_command)]
pub async fn message(
ctx: Context<'_>,
#[description = "The message to send"] message: String,
) -> Result<(), Error> {
let config = ctx.data().pair_config.lock().await;
let mut config = ctx.data().pair_config.lock().await;

config.to_channel_id = ctx.channel_id();

config
.from_channel_id
@@ -87,8 +85,5 @@ pub async fn message(
),
)
.await?;

ctx.defer_ephemeral().await?;
ctx.say("Sent message to paired server").await?;
Ok(())
}
29 changes: 17 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -7,11 +7,12 @@ use poise::{
serenity_prelude::{self as serenity, ChannelId, GuildId, UserId},
};
use std::env::var;
use tokio::sync::RwLock;

type Error = Box<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;

const CLYDE_ID: u64 = 1081004946872352958;

#[derive(Default)]
pub struct Data {
pair_config: Mutex<PairConfiguration>,
@@ -24,29 +25,30 @@ pub struct PairConfiguration {
to_channel_id: ChannelId,
}


#[derive(Default)]
struct Handler {
options: poise::FrameworkOptions<Data, Error>,
data: Data,
bot_id: RwLock<Option<UserId>>,
bot_id: UserId,
shard_manager:
std::sync::Mutex<Option<std::sync::Arc<tokio::sync::Mutex<serenity::ShardManager>>>>,
}

const CLYDE_ID: u64 = 1081004946872352958;

// Custom handler to dispatch poise events.
impl Handler {
pub fn new(options: poise::FrameworkOptions<Data, Error>) -> Self {
pub fn new(options: poise::FrameworkOptions<Data, Error>, bot_id: u64) -> Self {
Self {
options,
bot_id: UserId(bot_id),
..Default::default()
}
}

async fn dispatch_poise_event(&self, ctx: &serenity::Context, event: &poise::Event<'_>) {
let framework_data = poise::FrameworkContext {
bot_id: self.bot_id.read().await.unwrap(),
bot_id: self.bot_id,
options: &self.options,
user_data: &self.data,
shard_manager: &(*self.shard_manager.lock().unwrap()).clone().unwrap(),
@@ -62,7 +64,8 @@ impl serenity::EventHandler for Handler {
if new_message.author.bot && new_message.author.id == CLYDE_ID {
let data = self.data.pair_config.lock().await;

if data.to_channel_id == new_message.channel_id {
// TODO: If the message is in a thread check for the channel id in which the thread was.
if data.from_channel_id == new_message.channel_id {
let _ = data
.to_channel_id
.say(&ctx.http, &new_message.content)
@@ -78,10 +81,6 @@ impl serenity::EventHandler for Handler {
self.dispatch_poise_event(&ctx, &poise::Event::InteractionCreate { interaction })
.await;
}

async fn ready(&self, _ctx: serenity::Context, ready: serenity::Ready) {
*self.bot_id.write().await = Some(ready.user.id);
}
}

async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
@@ -107,7 +106,7 @@ async fn main() -> Result<(), Error> {
commands: vec![
commands::help(),
commands::register(),
commands::pair(),
commands::proxy(),
commands::message(),
],
prefix_options: poise::PrefixFrameworkOptions {
@@ -132,7 +131,13 @@ async fn main() -> Result<(), Error> {
..Default::default()
};

let handler = std::sync::Arc::new(Handler::new(options));
let handler = std::sync::Arc::new(Handler::new(
options,
var("SELF_BOT_USER_ID")
.expect("Missing `DISCORD_TOKEN` environment variable")
.parse()
.unwrap(),
));

let mut client = serenity::Client::builder(
var("DISCORD_TOKEN").expect("Missing `DISCORD_TOKEN` environment variable"),

0 comments on commit b77f9ff

Please sign in to comment.