feat: Implemented guild check
- Also added more error logging
This commit is contained in:
parent
d584340f01
commit
d188bba16e
8 changed files with 51 additions and 9 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
use poise::serenity_prelude::User;
|
use poise::serenity_prelude::User;
|
||||||
use squad_quest::{SquadObject, account::Account, map::Map};
|
use squad_quest::{SquadObject, account::Account, map::Map};
|
||||||
|
|
||||||
use crate::{Context, Error, account::{account_full_balance, account_user_id, fetch_or_init_account}, strings::StringFormatter};
|
use crate::{Context, Error, account::{account_full_balance, account_user_id, fetch_or_init_account}, strings::StringFormatter, commands::guild};
|
||||||
|
|
||||||
async fn account_balance_string(
|
async fn account_balance_string(
|
||||||
ctx: &Context<'_>,
|
ctx: &Context<'_>,
|
||||||
|
|
@ -26,6 +26,7 @@ async fn account_balance_string(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
name_localized("ru", "сбросить"),
|
name_localized("ru", "сбросить"),
|
||||||
description_localized("ru", "Сбросить аккаунт пользователя, вкл. баланс, открытые комнаты и пройденные квесты"),
|
description_localized("ru", "Сбросить аккаунт пользователя, вкл. баланс, открытые комнаты и пройденные квесты"),
|
||||||
|
|
@ -63,6 +64,7 @@ pub async fn reset(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "счет"),
|
name_localized("ru", "счет"),
|
||||||
description_localized("ru", "Отобразить таблицу лидеров"),
|
description_localized("ru", "Отобразить таблицу лидеров"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -106,6 +108,7 @@ pub async fn scoreboard(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
subcommands("give", "set"),
|
subcommands("give", "set"),
|
||||||
name_localized("ru", "баланс"),
|
name_localized("ru", "баланс"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -120,6 +123,7 @@ pub async fn balance(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "передать"),
|
name_localized("ru", "передать"),
|
||||||
description_localized("ru", "Передать очки другому пользователю"),
|
description_localized("ru", "Передать очки другому пользователю"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -178,6 +182,7 @@ pub async fn give(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
name_localized("ru", "установить"),
|
name_localized("ru", "установить"),
|
||||||
description_localized("ru", "Устанавливает текущий баланс пользователя"),
|
description_localized("ru", "Устанавливает текущий баланс пользователя"),
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
use poise::serenity_prelude::{Attachment, ComponentInteractionCollector, CreateActionRow, CreateAttachment, CreateButton, CreateMessage, EditMessage};
|
use poise::serenity_prelude::{Attachment, ComponentInteractionCollector, CreateActionRow, CreateAttachment, CreateButton, CreateMessage, EditMessage};
|
||||||
use squad_quest::SquadObject;
|
use squad_quest::SquadObject;
|
||||||
|
|
||||||
use crate::{Context, Error, account::fetch_or_init_account};
|
use crate::{Context, Error, account::fetch_or_init_account, commands::guild};
|
||||||
|
|
||||||
/// Send an answer to the quest for review
|
/// Send an answer to the quest for review
|
||||||
#[poise::command(
|
#[poise::command(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "ответить"),
|
name_localized("ru", "ответить"),
|
||||||
description_localized("ru", "Отправить ответ на квест на проверку"),
|
description_localized("ru", "Отправить ответ на квест на проверку"),
|
||||||
)]
|
)]
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use poise::{CreateReply, serenity_prelude::ChannelId};
|
||||||
use squad_quest::SquadObject;
|
use squad_quest::SquadObject;
|
||||||
use toml::value::Time;
|
use toml::value::Time;
|
||||||
|
|
||||||
use crate::{Context, Error, timer::DailyTimer};
|
use crate::{Context, Error, timer::DailyTimer, commands::guild};
|
||||||
|
|
||||||
/// Set channels to post quests and answers to
|
/// Set channels to post quests and answers to
|
||||||
#[poise::command(
|
#[poise::command(
|
||||||
|
|
@ -12,6 +12,7 @@ use crate::{Context, Error, timer::DailyTimer};
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "инит"),
|
name_localized("ru", "инит"),
|
||||||
description_localized("ru", "Установить каналы для публикации квестов и ответов"),
|
description_localized("ru", "Установить каналы для публикации квестов и ответов"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -75,6 +76,7 @@ fn seconds(time: Time) -> u64 {
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "таймер"),
|
name_localized("ru", "таймер"),
|
||||||
description_localized("ru", "Включить таймер публикации по заданной временной метке UTC (МСК -3)"),
|
description_localized("ru", "Включить таймер публикации по заданной временной метке UTC (МСК -3)"),
|
||||||
)]
|
)]
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
use squad_quest::{SquadObject, map::Map};
|
use squad_quest::{SquadObject, map::Map};
|
||||||
|
|
||||||
use crate::{Context, account::fetch_or_init_account, error::Error};
|
use crate::{Context, account::fetch_or_init_account, error::Error, commands::guild};
|
||||||
|
|
||||||
/// Unlock specified room if it is reachable and you have required amount of points
|
/// Unlock specified room if it is reachable and you have required amount of points
|
||||||
#[poise::command(
|
#[poise::command(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "открыть"),
|
name_localized("ru", "открыть"),
|
||||||
description_localized("ru", "Открывает указанную комнату, если хватает очков и до нее можно добраться"),
|
description_localized("ru", "Открывает указанную комнату, если хватает очков и до нее можно добраться"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -55,6 +56,7 @@ pub async fn unlock(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "пойти"),
|
name_localized("ru", "пойти"),
|
||||||
description_localized("ru", "Переместиться в другую разблокированную комнату"),
|
description_localized("ru", "Переместиться в другую разблокированную комнату"),
|
||||||
)]
|
)]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
use poise::CreateReply;
|
use poise::{CreateReply, serenity_prelude::GuildId};
|
||||||
|
|
||||||
use crate::{Context, Data, Error};
|
use crate::{Context, Data, Error};
|
||||||
|
|
||||||
|
|
@ -10,6 +10,18 @@ pub mod social;
|
||||||
pub mod account;
|
pub mod account;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
|
|
||||||
|
pub async fn guild(ctx: Context<'_>) -> Result<bool, Error> {
|
||||||
|
let id = ctx.guild_id().expect("guild-only command");
|
||||||
|
let guard = ctx.data().discord.lock().expect("shouldn't be locked");
|
||||||
|
let expected_id = guard.guild;
|
||||||
|
|
||||||
|
if expected_id != GuildId::default() && id != expected_id {
|
||||||
|
return Err(Error::NotThisGuild);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
#[poise::command(prefix_command)]
|
#[poise::command(prefix_command)]
|
||||||
pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
pub async fn register(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
poise::builtins::register_application_commands_buttons(ctx).await?;
|
poise::builtins::register_application_commands_buttons(ctx).await?;
|
||||||
|
|
@ -36,10 +48,17 @@ pub async fn error_handler(error: poise::FrameworkError<'_, Data, Error>) {
|
||||||
eprintln!("ERROR:");
|
eprintln!("ERROR:");
|
||||||
print_error_recursively(&error);
|
print_error_recursively(&error);
|
||||||
if let Some(ctx) = error.ctx() {
|
if let Some(ctx) = error.ctx() {
|
||||||
|
let user = ctx.author().display_name();
|
||||||
|
eprintln!("User: {user} ({id})", id = ctx.author().id);
|
||||||
|
|
||||||
let response = match error {
|
let response = match error {
|
||||||
poise::FrameworkError::Command { error, .. } => format!("Internal server error: {error}"),
|
poise::FrameworkError::Command { error, .. } => {
|
||||||
|
eprintln!("Invokation string: {}", ctx.invocation_string());
|
||||||
|
format!("Internal server error: {error}")
|
||||||
|
},
|
||||||
_ => format!("Internal server error: {error}"),
|
_ => format!("Internal server error: {error}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(error) = ctx.send(CreateReply::default().content(response).ephemeral(true)).await {
|
if let Err(error) = ctx.send(CreateReply::default().content(response).ephemeral(true)).await {
|
||||||
eprintln!("Couldn't send error message: {error}");
|
eprintln!("Couldn't send error message: {error}");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use std::{future, str::FromStr};
|
||||||
use poise::serenity_prelude::{CreateMessage, EditMessage, Message, futures::StreamExt};
|
use poise::serenity_prelude::{CreateMessage, EditMessage, Message, futures::StreamExt};
|
||||||
use squad_quest::{SquadObject, quest::{Quest, QuestDifficulty}};
|
use squad_quest::{SquadObject, quest::{Quest, QuestDifficulty}};
|
||||||
use toml::value::Date;
|
use toml::value::Date;
|
||||||
use crate::{Context, Error};
|
use crate::{Context, Error,commands::guild};
|
||||||
|
|
||||||
async fn find_quest_message(ctx: Context<'_>, id: u16) -> Result<Option<Message>, Error>{
|
async fn find_quest_message(ctx: Context<'_>, id: u16) -> Result<Option<Message>, Error>{
|
||||||
ctx.defer().await?;
|
ctx.defer().await?;
|
||||||
|
|
@ -34,6 +34,7 @@ fn make_quest_message_content(ctx: Context<'_>, quest: &Quest) -> String {
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
subcommands("list", "create", "update", "publish", "delete"),
|
subcommands("list", "create", "update", "publish", "delete"),
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
name_localized("ru", "квест"),
|
name_localized("ru", "квест"),
|
||||||
|
|
@ -49,6 +50,7 @@ pub async fn quest(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
name_localized("ru", "список"),
|
name_localized("ru", "список"),
|
||||||
description_localized("ru", "Вывести все квесты")
|
description_localized("ru", "Вывести все квесты")
|
||||||
|
|
@ -114,6 +116,7 @@ impl From<DateWrapper> for Date {
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "создать"),
|
name_localized("ru", "создать"),
|
||||||
description_localized("ru", "Создать квест и получить его идентификатор"),
|
description_localized("ru", "Создать квест и получить его идентификатор"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -202,6 +205,7 @@ pub async fn create(
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "обновить"),
|
name_localized("ru", "обновить"),
|
||||||
description_localized("ru", "Обновить выбранные значения указанного квеста"),
|
description_localized("ru", "Обновить выбранные значения указанного квеста"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -339,6 +343,7 @@ pub async fn publish_inner(ctx: Context<'_>, quest: &mut Quest) -> Result<Messag
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "опубликовать"),
|
name_localized("ru", "опубликовать"),
|
||||||
description_localized("ru", "Отметить квест как публичный и отправить его сообщение в канал квестов"),
|
description_localized("ru", "Отметить квест как публичный и отправить его сообщение в канал квестов"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -378,6 +383,7 @@ pub async fn publish(
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "удалить"),
|
name_localized("ru", "удалить"),
|
||||||
description_localized("ru", "Удалить квест (и его сообщение, если он был опубликован)"),
|
description_localized("ru", "Удалить квест (и его сообщение, если он был опубликован)"),
|
||||||
)]
|
)]
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
use poise::serenity_prelude::{Attachment, ChannelId, CreateAttachment, CreateMessage, EditMessage, MessageId, UserId};
|
use poise::serenity_prelude::{Attachment, ChannelId, CreateAttachment, CreateMessage, EditMessage, MessageId, UserId};
|
||||||
|
|
||||||
use crate::{Context, Error};
|
use crate::{Context, Error, commands::guild};
|
||||||
|
|
||||||
#[poise::command(
|
#[poise::command(
|
||||||
prefix_command,
|
prefix_command,
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
subcommands("msg", "edit", "undo"),
|
subcommands("msg", "edit", "undo"),
|
||||||
name_localized("ru", "сообщение"),
|
name_localized("ru", "сообщение"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -20,6 +21,7 @@ pub async fn social( _ctx: Context<'_> ) -> Result<(), Error> {
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "написать"),
|
name_localized("ru", "написать"),
|
||||||
description_localized("ru", "Отправить сообщение пользователю или в канал"),
|
description_localized("ru", "Отправить сообщение пользователю или в канал"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -98,6 +100,7 @@ pub async fn msg (
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "редактировать"),
|
name_localized("ru", "редактировать"),
|
||||||
description_localized("ru", "Редактировать сообщение в канале или в ЛС"),
|
description_localized("ru", "Редактировать сообщение в канале или в ЛС"),
|
||||||
)]
|
)]
|
||||||
|
|
@ -177,6 +180,7 @@ pub async fn edit (
|
||||||
slash_command,
|
slash_command,
|
||||||
required_permissions = "ADMINISTRATOR",
|
required_permissions = "ADMINISTRATOR",
|
||||||
guild_only,
|
guild_only,
|
||||||
|
check = "guild",
|
||||||
name_localized("ru", "удалить"),
|
name_localized("ru", "удалить"),
|
||||||
description_localized("ru", "Удалить сообщение в канале или в ЛС"),
|
description_localized("ru", "Удалить сообщение в канале или в ЛС"),
|
||||||
)]
|
)]
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ pub enum Error {
|
||||||
RoomAlreadyUnlocked(u16),
|
RoomAlreadyUnlocked(u16),
|
||||||
CannotReach(u16),
|
CannotReach(u16),
|
||||||
TimerSet,
|
TimerSet,
|
||||||
|
NotThisGuild,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<serenity::Error> for Error {
|
impl From<serenity::Error> for Error {
|
||||||
|
|
@ -65,6 +66,7 @@ impl Display for Error {
|
||||||
Self::RoomAlreadyUnlocked(id) => write!(f, "room #{id} is already unlocked for this user"),
|
Self::RoomAlreadyUnlocked(id) => write!(f, "room #{id} is already unlocked for this user"),
|
||||||
Self::CannotReach(id) => write!(f, "user cannot reach room #{id}"),
|
Self::CannotReach(id) => write!(f, "user cannot reach room #{id}"),
|
||||||
Self::TimerSet => write!(f, "timer is already set"),
|
Self::TimerSet => write!(f, "timer is already set"),
|
||||||
|
Self::NotThisGuild => write!(f, "cannot be used in this guild"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +86,8 @@ impl std::error::Error for Error {
|
||||||
Self::RoomNotFound(_) |
|
Self::RoomNotFound(_) |
|
||||||
Self::RoomAlreadyUnlocked(_) |
|
Self::RoomAlreadyUnlocked(_) |
|
||||||
Self::CannotReach(_) |
|
Self::CannotReach(_) |
|
||||||
Self::TimerSet => None,
|
Self::TimerSet |
|
||||||
|
Self::NotThisGuild => None,
|
||||||
Self::SerenityError(error) => Some(error),
|
Self::SerenityError(error) => Some(error),
|
||||||
Self::SquadQuestError(error) => Some(error),
|
Self::SquadQuestError(error) => Some(error),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue