mirror of
https://github.com/kurbezz/discord-bot.git
synced 2026-03-04 00:20:48 +01:00
Update
This commit is contained in:
@@ -1,13 +0,0 @@
|
|||||||
use serenity::{all::CommandOptionType, builder::*};
|
|
||||||
|
|
||||||
|
|
||||||
pub fn register() -> CreateCommand {
|
|
||||||
CreateCommand::new("copy_message")
|
|
||||||
.description("Не вызывать, только для настройки")
|
|
||||||
.add_option(
|
|
||||||
CreateCommandOption::new(
|
|
||||||
CommandOptionType::String, "message_id", "ID сообщения"
|
|
||||||
)
|
|
||||||
.required(true)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,2 @@
|
|||||||
pub mod add_game;
|
pub mod add_game;
|
||||||
pub mod delete_game;
|
pub mod delete_game;
|
||||||
pub mod copy_message;
|
|
||||||
|
|||||||
37
src/main.rs
37
src/main.rs
@@ -1,5 +1,5 @@
|
|||||||
use reqwest::Url;
|
use reqwest::Url;
|
||||||
use serenity::all::{ActivityData, AutocompleteChoice, CommandOptionType, CreateAutocompleteResponse, CreateCommandOption, CreateInteractionResponse, CreateInteractionResponseMessage, EditMessage, GuildId, Interaction};
|
use serenity::all::{ActivityData, AutocompleteChoice, CreateAutocompleteResponse, CreateInteractionResponse, CreateInteractionResponseMessage, EditMessage, GuildId, Interaction};
|
||||||
use serenity::async_trait;
|
use serenity::async_trait;
|
||||||
use serenity::model::channel::Message;
|
use serenity::model::channel::Message;
|
||||||
use serenity::prelude::*;
|
use serenity::prelude::*;
|
||||||
@@ -37,16 +37,6 @@ impl EventHandler for Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
match command.data.name.as_str() {
|
match command.data.name.as_str() {
|
||||||
"copy_message" => {
|
|
||||||
let message_id = command.data.options[0].value.as_str().unwrap().parse::<u64>().unwrap();
|
|
||||||
let message = command.channel_id.message(&ctx.http, message_id).await.unwrap();
|
|
||||||
|
|
||||||
let data = CreateInteractionResponseMessage::new().content(message.content);
|
|
||||||
let builder = CreateInteractionResponse::Message(data);
|
|
||||||
if let Err(why) = command.create_response(&ctx.http, builder).await {
|
|
||||||
println!("Cannot respond to slash command: {why}");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"add" => {
|
"add" => {
|
||||||
let mut message = command.channel_id.message(&ctx.http, config::CONFIG.discord_game_list_message_id).await.unwrap();
|
let mut message = command.channel_id.message(&ctx.http, config::CONFIG.discord_game_list_message_id).await.unwrap();
|
||||||
|
|
||||||
@@ -106,23 +96,23 @@ impl EventHandler for Handler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Received autocomplete interaction: {interaction:#?}");
|
if interaction.data.name.as_str() == "delete" {
|
||||||
|
let message = interaction.channel_id.message(&ctx.http, config::CONFIG.discord_game_list_message_id).await.unwrap();
|
||||||
|
let categories = parse_games_list(&message.content).await;
|
||||||
|
let games = categories.iter().flat_map(|category| category.games.iter()).collect::<Vec<&String>>();
|
||||||
|
|
||||||
match interaction.data.name.as_str() {
|
let query = interaction.data.options[0].value.as_str().unwrap();
|
||||||
"game" => {
|
|
||||||
let message = interaction.channel_id.message(&ctx.http, config::CONFIG.discord_game_list_message_id).await.unwrap();
|
|
||||||
let categories = parse_games_list(&message.content).await;
|
|
||||||
let games = categories.iter().flat_map(|category| category.games.iter()).collect::<Vec<&String>>();
|
|
||||||
|
|
||||||
let autocompolete_response = CreateAutocompleteResponse::new().set_choices(
|
let autocompolete_response = CreateAutocompleteResponse::new().set_choices(
|
||||||
games.iter().map(|game| {
|
games
|
||||||
|
.iter()
|
||||||
|
.filter(|game| game.to_lowercase().contains(&query.to_lowercase()))
|
||||||
|
.map(|game| {
|
||||||
AutocompleteChoice::new(game.to_string(), game.to_string())
|
AutocompleteChoice::new(game.to_string(), game.to_string())
|
||||||
}).collect()
|
}).collect()
|
||||||
);
|
);
|
||||||
|
|
||||||
let _ = interaction.create_response(&ctx.http, serenity::builder::CreateInteractionResponse::Autocomplete(autocompolete_response)).await.unwrap();
|
let _ = interaction.create_response(&ctx.http, serenity::builder::CreateInteractionResponse::Autocomplete(autocompolete_response)).await.unwrap();
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -147,7 +137,6 @@ impl EventHandler for Handler {
|
|||||||
vec![
|
vec![
|
||||||
commands::add_game::register(),
|
commands::add_game::register(),
|
||||||
commands::delete_game::register(),
|
commands::delete_game::register(),
|
||||||
commands::copy_message::register(),
|
|
||||||
]
|
]
|
||||||
).await.unwrap();
|
).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
38
src/utils.rs
38
src/utils.rs
@@ -1,6 +1,3 @@
|
|||||||
use std::vec;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Category {
|
pub struct Category {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -11,31 +8,26 @@ pub struct Category {
|
|||||||
pub async fn parse_games_list(text: &str) -> Vec<Category> {
|
pub async fn parse_games_list(text: &str) -> Vec<Category> {
|
||||||
let mut categories = vec![];
|
let mut categories = vec![];
|
||||||
|
|
||||||
let lines = text.lines();
|
for line in text.lines() {
|
||||||
|
|
||||||
let mut current_category: Option<Category> = None;
|
|
||||||
|
|
||||||
for line in lines.into_iter() {
|
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if line.starts_with("* ") {
|
if !line.starts_with("* ") {
|
||||||
current_category.clone().unwrap().games.push(line.to_string());
|
let category_name = line;
|
||||||
} else {
|
let category = Category {
|
||||||
if let Some(category) = current_category {
|
name: category_name.to_string(),
|
||||||
categories.push(category);
|
|
||||||
}
|
|
||||||
|
|
||||||
current_category = Some(Category {
|
|
||||||
name: line.to_string(),
|
|
||||||
games: vec![]
|
games: vec![]
|
||||||
});
|
};
|
||||||
|
|
||||||
|
categories.push(category);
|
||||||
|
} else {
|
||||||
|
let game_line = line.trim();
|
||||||
|
let last_category = categories.last_mut().unwrap();
|
||||||
|
last_category.games.push(game_line.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
categories.push(current_category.unwrap());
|
|
||||||
|
|
||||||
categories
|
categories
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,10 +51,8 @@ pub async fn delete_game(
|
|||||||
mut categories: Vec<Category>,
|
mut categories: Vec<Category>,
|
||||||
game_name: &str
|
game_name: &str
|
||||||
) -> Vec<Category> {
|
) -> Vec<Category> {
|
||||||
let prefix = format!("* {}", game_name);
|
|
||||||
|
|
||||||
for category in categories.iter_mut() {
|
for category in categories.iter_mut() {
|
||||||
category.games.retain(|game| !game.starts_with(&prefix));
|
category.games.retain(|game| !game.starts_with(game_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
categories
|
categories
|
||||||
@@ -76,7 +66,7 @@ pub async fn format_games_list(categories: Vec<Category>) -> String {
|
|||||||
result.push_str(&format!("{}\n", category.name));
|
result.push_str(&format!("{}\n", category.name));
|
||||||
|
|
||||||
for game in category.games.iter() {
|
for game in category.games.iter() {
|
||||||
result.push_str(&format!("* {}\n", game));
|
result.push_str(&format!("{}\n", game));
|
||||||
}
|
}
|
||||||
result.push_str("\n\n");
|
result.push_str("\n\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user