This commit is contained in:
2024-05-13 23:43:26 +02:00
parent ee0040ddbb
commit 553adb59a7
4 changed files with 27 additions and 62 deletions

View File

@@ -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)
)
}

View File

@@ -1,3 +1,2 @@
pub mod add_game;
pub mod delete_game;
pub mod copy_message;

View File

@@ -1,5 +1,5 @@
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::model::channel::Message;
use serenity::prelude::*;
@@ -37,16 +37,6 @@ impl EventHandler for Handler {
}
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" => {
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;
}
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() {
"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 query = interaction.data.options[0].value.as_str().unwrap();
let autocompolete_response = CreateAutocompleteResponse::new().set_choices(
games.iter().map(|game| {
let autocompolete_response = CreateAutocompleteResponse::new().set_choices(
games
.iter()
.filter(|game| game.to_lowercase().contains(&query.to_lowercase()))
.map(|game| {
AutocompleteChoice::new(game.to_string(), game.to_string())
}).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![
commands::add_game::register(),
commands::delete_game::register(),
commands::copy_message::register(),
]
).await.unwrap();
}

View File

@@ -1,6 +1,3 @@
use std::vec;
#[derive(Clone)]
pub struct Category {
pub name: String,
@@ -11,31 +8,26 @@ pub struct Category {
pub async fn parse_games_list(text: &str) -> Vec<Category> {
let mut categories = vec![];
let lines = text.lines();
let mut current_category: Option<Category> = None;
for line in lines.into_iter() {
for line in text.lines() {
if line.is_empty() {
continue;
}
if line.starts_with("* ") {
current_category.clone().unwrap().games.push(line.to_string());
} else {
if let Some(category) = current_category {
categories.push(category);
}
current_category = Some(Category {
name: line.to_string(),
if !line.starts_with("* ") {
let category_name = line;
let category = Category {
name: category_name.to_string(),
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
}
@@ -59,10 +51,8 @@ pub async fn delete_game(
mut categories: Vec<Category>,
game_name: &str
) -> Vec<Category> {
let prefix = format!("* {}", game_name);
for category in categories.iter_mut() {
category.games.retain(|game| !game.starts_with(&prefix));
category.games.retain(|game| !game.starts_with(game_name));
}
categories
@@ -76,7 +66,7 @@ pub async fn format_games_list(categories: Vec<Category>) -> String {
result.push_str(&format!("{}\n", category.name));
for game in category.games.iter() {
result.push_str(&format!("* {}\n", game));
result.push_str(&format!("{}\n", game));
}
result.push_str("\n\n");
}