-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
slash command implementation #8
Open
DanceMore
wants to merge
1
commit into
main
Choose a base branch
from
slash-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use serenity::all::standard::macros::command; | ||
use serenity::all::standard::CommandResult; | ||
use serenity::all::CommandOptionType; | ||
use serenity::all::Context; | ||
use serenity::all::CreateCommand; | ||
use serenity::all::CreateCommandOption; | ||
use serenity::all::Message; | ||
|
||
use discord_bridgebot::establish_connection; | ||
use discord_bridgebot::models::*; | ||
use std::num::NonZeroU64; | ||
|
||
use diesel::RunQueryDsl; | ||
|
||
#[command] | ||
pub async fn run(ctx: &Context, msg: &Message) -> CommandResult { | ||
println!("[-] inside bridge registration"); | ||
|
||
// it seems like limiting the scope of the imports is important? | ||
// I can definitely break code with name-conflicts by putting this at the top.... | ||
use discord_bridgebot::schema::channel_pairs; | ||
let connection = &mut establish_connection(); | ||
|
||
// Extract the text content of the message | ||
let mut iter = msg.content.split_whitespace(); | ||
let _ = iter.next(); // Skip the command (!register) | ||
|
||
// Get the second element (Channel ID) | ||
let channel_id_str = iter.next(); | ||
|
||
// Declare channel2 outside the block | ||
let channel2: i64; | ||
|
||
// check that we have a Channel ID | ||
if let Some(channel_id_str) = channel_id_str { | ||
// Attempt to parse the text as an i64 | ||
channel2 = match channel_id_str.parse() { | ||
Ok(id) => id, | ||
Err(_) => { | ||
msg.reply(ctx, "Invalid ChannelID format").await?; | ||
return Ok(()); | ||
} | ||
}; | ||
} else { | ||
msg.reply(ctx, "Invalid register command format").await?; | ||
return Ok(()); | ||
} | ||
|
||
// Get the ID of the channel where the message was sent | ||
let channel1 = msg.channel_id; | ||
|
||
let channel2_o = | ||
serenity::model::id::ChannelId::from(NonZeroU64::new(channel2 as u64).unwrap()); | ||
|
||
// make sure we can see the channel target... | ||
match ctx.http.get_channel(channel2_o).await { | ||
Ok(_channel) => { | ||
// alright, since we're here, let's create and save the channel to datbase. | ||
let new_pair = ChannelPair { | ||
id: None, // Omitting the id because it's auto-incremented | ||
channel1: channel1.into(), | ||
channel2: channel2, | ||
}; | ||
|
||
let result = diesel::insert_into(channel_pairs::table) | ||
.values(&new_pair) | ||
.execute(connection); | ||
|
||
match result { | ||
Ok(_) => { | ||
msg.reply(ctx, "Registration successful").await?; | ||
} | ||
Err(_) => { | ||
msg.reply(ctx, "Error registering the ChannelID").await?; | ||
} | ||
} | ||
} | ||
Err(_) => { | ||
msg.reply( | ||
ctx, | ||
format!("I don't think I can see ChannelID `{}`", channel2), | ||
) | ||
.await?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn register() -> CreateCommand { | ||
CreateCommand::new("bridge") | ||
.description("Bridge the current ChannelID to the $channel_id ChannelID") | ||
.add_option( | ||
CreateCommandOption::new( | ||
CommandOptionType::String, | ||
"channel_id", | ||
"the target channel for the bridge, by ChannelID", | ||
) | ||
.required(true), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod bridge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / clippy
redundant field names in struct initialization Warning