123 lines
3.4 KiB
Rust
123 lines
3.4 KiB
Rust
use crate::{Context, Error};
|
|
use poise::CreateReply;
|
|
use rusqlite::Connection;
|
|
use serenity::all::{CreateEmbed, GuildChannel};
|
|
|
|
// struct Forum {
|
|
// id: i64,
|
|
// guild_id: String,
|
|
// forum_id: String,
|
|
// }
|
|
|
|
#[poise::command(slash_command, subcommands("add", "delete"), guild_only)]
|
|
pub async fn forum(_ctx: Context<'_>) -> Result<(), Error> {
|
|
Ok(())
|
|
}
|
|
|
|
fn forum_exists<S: Into<String>>(
|
|
conn: &Connection,
|
|
forum_id: S,
|
|
guild_id: S,
|
|
) -> Result<bool, Error> {
|
|
let exists: bool = conn.query_row(
|
|
"SELECT EXISTS(SELECT 1 FROM forums WHERE forum_id = ?1 AND guild_id = ?2)",
|
|
[forum_id.into(), guild_id.into()],
|
|
|row| row.get(0),
|
|
)?;
|
|
|
|
Ok(exists)
|
|
}
|
|
|
|
/// Add forum
|
|
#[poise::command(
|
|
prefix_command,
|
|
slash_command,
|
|
required_permissions = "MANAGE_MESSAGES"
|
|
)]
|
|
pub async fn add(
|
|
ctx: Context<'_>,
|
|
#[description = "FAQ Title"]
|
|
#[channel_types("Forum")]
|
|
forum_channel: GuildChannel,
|
|
) -> Result<(), Error> {
|
|
let data = ctx.data();
|
|
|
|
let result = data
|
|
.execute_db({
|
|
let guild_id = ctx.guild_id().expect("Guild ID not found").to_string();
|
|
move |conn| {
|
|
let exists = forum_exists(conn, forum_channel.id.to_string(), guild_id.clone())
|
|
.unwrap_or(false);
|
|
|
|
// add
|
|
if !exists {
|
|
conn.execute(
|
|
"INSERT INTO forums (guild_id, forum_id) VALUES (?1, ?2)",
|
|
[&guild_id, &forum_channel.id.to_string()],
|
|
)
|
|
} else {
|
|
Ok(0)
|
|
}
|
|
}
|
|
})
|
|
.await?;
|
|
|
|
if result == 1 {
|
|
let embed = CreateEmbed::new()
|
|
.title("Added new forum channel to monitor")
|
|
.description(format!("<#{}>", forum_channel.id));
|
|
ctx.send(CreateReply::default().embed(embed)).await?;
|
|
} else {
|
|
let embed = CreateEmbed::new()
|
|
.title("Error adding forum channel")
|
|
.description("Forum channel already exists!");
|
|
ctx.send(CreateReply::default().embed(embed)).await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Delete forum
|
|
#[poise::command(
|
|
prefix_command,
|
|
slash_command,
|
|
required_permissions = "MANAGE_MESSAGES"
|
|
)]
|
|
pub async fn delete(
|
|
ctx: Context<'_>,
|
|
#[description = "Forum channel"]
|
|
#[channel_types("Forum")]
|
|
forum_channel: GuildChannel,
|
|
) -> Result<(), Error> {
|
|
let data = ctx.data();
|
|
let server_guild_id = ctx.guild_id().expect("Guild ID not found");
|
|
let guild_id = server_guild_id.to_string();
|
|
|
|
let result = data
|
|
.execute_db({
|
|
let forum_id = forum_channel.id.to_string();
|
|
move |conn| {
|
|
conn.execute(
|
|
"DELETE FROM faqs WHERE forum_id = ?1 AND guild_id = ?2",
|
|
[forum_id, guild_id],
|
|
)
|
|
}
|
|
})
|
|
.await?;
|
|
|
|
if result == 1 {
|
|
let embed = CreateEmbed::new()
|
|
.title("Deleted Forum")
|
|
.description(format!(
|
|
"Forum <#{}> has been removed from the database",
|
|
forum_channel.id
|
|
));
|
|
ctx.send(CreateReply::default().embed(embed)).await?;
|
|
} else {
|
|
let embed = CreateEmbed::new()
|
|
.title("Error deleting forum")
|
|
.description(format!("{} rows were affected", result));
|
|
ctx.send(CreateReply::default().embed(embed)).await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|