Fix git and also replace functions calls with borrowered string because why did I even code it otherwise in the first place
Run cargo test / Run tests (push) Successful in 3m44s
Build Docker Package / build (push) Successful in 12m10s

This commit is contained in:
Elias Wendland
2026-07-23 17:30:56 +02:00
parent 4298cf385b
commit a22f5781c2
11 changed files with 89 additions and 93 deletions
+16 -16
View File
@@ -34,7 +34,7 @@ pub async fn prompt(
Err(e) => {
warn!("TG_BOT_OLLAMA_HOST environment variable missing: {}", e);
let err_msg = "Error: Expected an ollama url in the environment (`TG_BOT_OLLAMA_HOST`).";
trace_message(err_msg.to_string(), channel_str.clone(), guild_str.clone()).await;
trace_message(err_msg, channel_str.clone(), guild_str.clone()).await;
ctx.say(err_msg).await?;
return Ok(());
}
@@ -65,7 +65,7 @@ pub async fn prompt(
Err(e) => {
error!("Failed to connect to Ollama server at {}: {}", formatted_host, e);
let err_msg = format!("Error: Failed to connect to Ollama: {}", e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
ctx.say(err_msg).await?;
return Ok(());
}
@@ -103,7 +103,7 @@ pub async fn prompt(
trace!("Generated system prompt for model creation (length: {} chars)", system_prompt.len());
trace!("System prompt content: {}", system_prompt);
let notice_msg = "EwiAI model is missing or out of date. Creating/updating model (this may take a bit)...";
trace_message(notice_msg.to_string(), channel_str.clone(), guild_str.clone()).await;
trace_message(notice_msg, channel_str.clone(), guild_str.clone()).await;
let _ = ctx.say(notice_msg).await?;
trace!("Sending create_model request to Ollama for 'EwiAI' based on 'gemma4:e2b-it-qat'...");
@@ -112,7 +112,7 @@ pub async fn prompt(
.from_model("gemma4:e2b-it-qat".into())).await {
error!("Failed to create model EwiAI: {}", e);
let err_msg = format!("Error creating model: {}", e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
ctx.say(err_msg).await?;
return Ok(());
}
@@ -155,7 +155,7 @@ pub async fn prompt(
trace!("Final prompt: {}", final_prompt);
let thinking_msg = "Thinking...\n-# The hardware this thing runs is really slow, expect a long wait";
trace_message(thinking_msg.to_string(), channel_str.clone(), guild_str.clone()).await;
trace_message(thinking_msg, channel_str.clone(), guild_str.clone()).await;
trace!("Sending initial thinking message...");
let reply = ctx.say(thinking_msg).await?;
@@ -169,7 +169,7 @@ pub async fn prompt(
Err(e) => {
error!("Error starting generation stream with Ollama: {}", e);
let err_msg = format!("Error during generation: {}", e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
reply.edit(ctx, CreateReply::default().content(err_msg)).await?;
return Ok(());
}
@@ -204,7 +204,7 @@ pub async fn prompt(
Err(e) => {
error!("Error encountered while reading stream chunk: {}", e);
let err_msg = format!("{} [Stream Error: {}]", response_text, e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
let _ = reply.edit(ctx, CreateReply::default().content(err_msg)).await;
return Ok(());
}
@@ -229,7 +229,7 @@ pub async fn prompt(
trace!("No final response stats available from stream");
}
trace_message(response_text.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&response_text, channel_str.clone(), guild_str.clone()).await;
trace!("Sending final edit to reply message...");
let _ = reply.edit(ctx, CreateReply::default().content(&response_text)).await;
debug!("prompt command finished successfully for user {}", ctx.author().name);
@@ -259,7 +259,7 @@ pub async fn answer(
Err(e) => {
warn!("TG_BOT_OLLAMA_HOST environment variable missing: {}", e);
let err_msg = "Error: Expected an ollama url in the environment (`TG_BOT_OLLAMA_HOST`).";
trace_message(err_msg.to_string(), channel_str.clone(), guild_str.clone()).await;
trace_message(err_msg, channel_str.clone(), guild_str.clone()).await;
ctx.say(err_msg).await?;
return Ok(());
}
@@ -290,7 +290,7 @@ pub async fn answer(
Err(e) => {
error!("Failed to connect to Ollama server at {}: {}", formatted_host, e);
let err_msg = format!("Error: Failed to connect to Ollama: {}", e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
ctx.say(err_msg).await?;
return Ok(());
}
@@ -328,7 +328,7 @@ pub async fn answer(
trace!("Generated system prompt for model creation (length: {} chars)", system_prompt.len());
trace!("System prompt content: {}", system_prompt);
let notice_msg = "EwiAI model is missing or out of date. Creating/updating model (this may take a bit)...";
trace_message(notice_msg.to_string(), channel_str.clone(), guild_str.clone()).await;
trace_message(notice_msg, channel_str.clone(), guild_str.clone()).await;
let _ = ctx.say(notice_msg).await?;
trace!("Sending create_model request to Ollama for 'EwiAI' based on 'gemma4:e2b-it-qat'...");
@@ -337,7 +337,7 @@ pub async fn answer(
.from_model("gemma4:e2b-it-qat".into())).await {
error!("Failed to create model EwiAI: {}", e);
let err_msg = format!("Error creating model: {}", e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
ctx.say(err_msg).await?;
return Ok(());
}
@@ -379,7 +379,7 @@ pub async fn answer(
trace!("Final prompt: {}", final_prompt);
let thinking_msg = "Thinking...\n-# The hardware this thing runs is really slow, expect a long wait";
trace_message(thinking_msg.to_string(), channel_str.clone(), guild_str.clone()).await;
trace_message(thinking_msg, channel_str.clone(), guild_str.clone()).await;
trace!("Sending initial thinking message...");
let reply = ctx.say(thinking_msg).await?;
@@ -393,7 +393,7 @@ pub async fn answer(
Err(e) => {
error!("Error starting generation stream with Ollama: {}", e);
let err_msg = format!("Error during generation: {}", e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
reply.edit(ctx, CreateReply::default().content(err_msg)).await?;
return Ok(());
}
@@ -428,7 +428,7 @@ pub async fn answer(
Err(e) => {
error!("Error encountered while reading stream chunk: {}", e);
let err_msg = format!("{} [Stream Error: {}]", response_text, e);
trace_message(err_msg.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&err_msg, channel_str.clone(), guild_str.clone()).await;
let _ = reply.edit(ctx, CreateReply::default().content(err_msg)).await;
return Ok(());
}
@@ -453,7 +453,7 @@ pub async fn answer(
trace!("No final response stats available from stream");
}
trace_message(response_text.clone(), channel_str.clone(), guild_str.clone()).await;
trace_message(&response_text, channel_str.clone(), guild_str.clone()).await;
trace!("Sending final edit to reply message...");
let _ = reply.edit(ctx, CreateReply::default().content(&response_text)).await;
debug!("answer command finished successfully for user {}", ctx.author().name);
+9 -16
View File
@@ -1,6 +1,6 @@
use crate::{Context, Error};
use tracing::{debug, trace, warn};
use crate::messaging::trace_message;
use crate::messaging::{edit_response_message, trace_message};
use regex::Regex;
use std::sync::LazyLock;
use std::str::FromStr;
@@ -138,15 +138,15 @@ pub async fn calc(ctx: Context<'_>,
) -> Result<(), Error> {
debug!("{} has requested to calculate {}", ctx.author().name, expression);
let msg = "Calculating...".to_string();
trace_message(msg.clone(), ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
trace_message(&msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
let response_message = ctx.say(msg).await?;
let start_time = std::time::Instant::now();
trace!("Saving start time: {:?}", start_time);
if expression.contains('=') {
let error_msg = format!("Invalid expression: {}\nYou are not allowed to have an equal sign in the expression.", expression);
debug!("Invalid expression detected: {}", expression);
trace_message(error_msg.clone(), ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
edit_response_message(&response_message, ctx, error_msg, false).await?;
trace_message(&error_msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
edit_response_message(&response_message, ctx, &error_msg, false).await?;
return Ok(());
}
let processed_expr : String = FIND_SCI_NOTATION_RE.replace_all(&expression, "$1 * 10^($2)").to_string();
@@ -157,8 +157,8 @@ pub async fn calc(ctx: Context<'_>,
Err(err_msg) => {
let error_msg = format!("Failed to parse or evaluate expression: `{}`", err_msg);
warn!("Failed to parse or evaluate expression: `{}` in guild {} channel {} by {}", err_msg, ctx.guild_id().unwrap().get(), ctx.channel_id().get(), ctx.author().name);
trace_message(error_msg.clone(), ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
edit_response_message(&response_message, ctx, error_msg, false).await?;
trace_message(&error_msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
edit_response_message(&response_message, ctx, &error_msg, false).await?;
return Ok(());
}
};
@@ -182,20 +182,13 @@ pub async fn calc(ctx: Context<'_>,
};
let msg = format!("{} = {} \n-# Precision: {} Compute time : {:?}", expression, value_string, precision_str, duration);
trace_message(msg.clone(), ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
edit_response_message(&response_message, ctx, msg, false).await?;
trace_message(&msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
edit_response_message(&response_message, ctx, &msg, false).await?;
debug!("Calculation perfomed for {} with result {} in {:?} by {}", expression, value_string, duration, ctx.author().name);
Ok(())
}
async fn edit_response_message<'a>(response_message: &poise::ReplyHandle<'a>, ctx: Context<'_>, content: String, silent: bool) -> Result<(), Error> {
if silent {
response_message.edit(ctx, poise::CreateReply::default().content(content).allowed_mentions(serenity::all::CreateAllowedMentions::new().empty_users())).await?;
} else {
response_message.edit(ctx, poise::CreateReply::default().content(content)).await?;
}
Ok(())
}
fn to_scientific_notation(value: String) -> String {
trace!("Converting {} to scientific notation", value);
+4 -4
View File
@@ -9,7 +9,7 @@ pub async fn fastfetch(ctx: Context<'_>) -> Result<(), Error> {
debug!("fastfetch command called by user {} in guild {}", ctx.author().id.get(), ctx.guild_id().unwrap().get());
let msg = "Processing...".to_string();
let res_msg = ctx.say(&msg).await?;
trace_message(msg.clone(), ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
trace_message(&msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
let cmd = Command::new("fastfetch").args(&["--raw", "true", "--logo", "none"]).output();
@@ -17,14 +17,14 @@ pub async fn fastfetch(ctx: Context<'_>) -> Result<(), Error> {
Err(e) => {
warn!("Error executing fastfetch: {}", e);
let msg = format!("Error: {}", e);
edit_response_message(&res_msg, ctx, msg.clone(), false).await?;
trace_message(msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
edit_response_message(&res_msg, ctx, &msg, false).await?;
trace_message(&msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
Ok(())
}
Ok(output) => {
let output_str = output.stdout.into_iter().map(|c| c as char).collect::<String>();
let msg = format!("```ansi\n{}\n```", output_str);
edit_response_message(&res_msg, ctx, msg.clone(), false).await?;
edit_response_message(&res_msg, ctx, &msg, false).await?;
// Tracing the output would be a bad idea since it's really long and filled with ansi escape codes
// trace_message(msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
trace!("Saying fastfetch command output");
+11 -11
View File
@@ -27,19 +27,19 @@ pub async fn find_prime(
let msg = "Calculating...".to_string();
let res_msg = ctx.say(msg.clone()).await?;
trace_message(msg, ctx.channel_id().to_string(), guild_id_str.clone()).await;
trace_message(&msg, ctx.channel_id().to_string(), guild_id_str.clone()).await;
if n == 0 {
let msg = "Prime indices start at 1. Please provide a value greater than 0.".to_string();
edit_response_message(&res_msg, ctx, msg.clone(), false).await?;
trace_message(msg, ctx.channel_id().to_string(), guild_id_str).await;
edit_response_message(&res_msg, ctx, &msg, false).await?;
trace_message(&msg, ctx.channel_id().to_string(), guild_id_str).await;
return Ok(());
}
if timeout.is_some() && timeout.unwrap() > 60 {
let msg = "Timeout is too long. Maximum is 60 seconds.".to_string();
edit_response_message(&res_msg, ctx, msg.clone(), false).await?;
trace_message(msg, ctx.channel_id().to_string(), guild_id_str).await;
edit_response_message(&res_msg, ctx, &msg, false).await?;
trace_message(&msg, ctx.channel_id().to_string(), guild_id_str).await;
return Ok(());
}
@@ -66,14 +66,14 @@ pub async fn find_prime(
Ok(Ok(result)) => result,
Ok(Err(_)) => {
let msg = "Calculation thread panicked or was dropped unexpectedly.".to_string();
edit_response_message(&res_msg, ctx, msg.clone(), false).await?;
trace_message(msg, ctx.channel_id().to_string(), guild_id_str).await;
edit_response_message(&res_msg, ctx, &msg, false).await?;
trace_message(&msg, ctx.channel_id().to_string(), guild_id_str).await;
return Ok(());
}
Err(_) => {
let msg = format!("Calculation timed out after {:?}", timeout_duration);
edit_response_message(&res_msg, ctx, msg.clone(), false).await?;
trace_message(msg, ctx.channel_id().to_string(), guild_id_str).await;
edit_response_message(&res_msg, ctx, &msg, false).await?;
trace_message(&msg, ctx.channel_id().to_string(), guild_id_str).await;
return Ok(());
}
};
@@ -81,8 +81,8 @@ pub async fn find_prime(
let duration = start_time.elapsed();
let msg = format!("The {}th prime number is {}\n-# Calculation time: {:?}, Method: {:?}", n, result, duration, method_name);
edit_response_message(&res_msg, ctx, msg.clone(), false).await?;
trace_message(msg, ctx.channel_id().to_string(), guild_id_str).await;
edit_response_message(&res_msg, ctx, &msg, false).await?;
trace_message(&msg, ctx.channel_id().to_string(), guild_id_str).await;
Ok(())
}
+5 -2
View File
@@ -1,5 +1,5 @@
use crate::{Context, Error};
use crate::messaging::trace_message;
use crate::messaging::{edit_response_message, trace_message};
use std::env;
use tracing::debug;
@@ -8,10 +8,13 @@ use tracing::debug;
pub async fn git(ctx: Context<'_>) -> Result<(), Error> {
debug!("git command ran by {} in {}", ctx.author().name, ctx.channel_id().get());
let response = ctx.say("Processing...").await?;
let msg = format!("Git repo: https://git.ewenlau.net/ewenlau/tg-dev-srv-bot\nIf you'd like to contribute, contact me to get an account.");
trace_message(msg, ctx.channel_id().get().to_string(), ctx.guild_id().unwrap().get().to_string()).await;
edit_response_message(&response, ctx, &msg, false).await?;
trace_message(&msg, ctx.channel_id().get().to_string(), ctx.guild_id().unwrap().get().to_string()).await;
Ok(())
+19 -19
View File
@@ -46,14 +46,14 @@ pub async fn manage_admins(ctx: Context<'_>,
if let Some(s_user) = user {
add_admin_user(ctx, &response_message, s_user).await?;
} else {
edit_response_message(&response_message, ctx, "Please provide a user".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide a user", false).await?;
}
}
Some(AdminOperationType::RemoveUser) => {
if let Some(s_user) = user {
remove_admin_user(ctx, &response_message, s_user).await?;
} else {
edit_response_message(&response_message, ctx, "Please provide a user".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide a user", false).await?;
}
}
Some(AdminOperationType::ListUsers) => {
@@ -63,21 +63,21 @@ pub async fn manage_admins(ctx: Context<'_>,
if let Some(s_role) = role {
add_admin_role(ctx, &response_message, s_role).await?;
} else {
edit_response_message(&response_message, ctx, "Please provide a role".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide a role", false).await?;
}
}
Some(AdminOperationType::RemoveRole) => {
if let Some(s_role) = role {
remove_admin_role(ctx, &response_message, s_role).await?;
} else {
edit_response_message(&response_message, ctx, "Please provide a role".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide a role", false).await?;
}
}
Some(AdminOperationType::ListRoles) => {
list_admin_roles(ctx, &response_message).await?;
}
None => {
edit_response_message(&response_message, ctx, "Please provide an operation".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide an operation", false).await?;
}
}
Ok(())
@@ -99,18 +99,18 @@ async fn add_admin_user<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>, user
trace!("Executing add_admin_user for user {}", user.user.name);
if is_target_admin_user(ctx, &user).await? {
trace!("User {} is already an admin", user.user.name);
edit_response_message(msg, ctx, format!("{} is already an admin", user.user.name), false).await?;
edit_response_message(msg, ctx, &format!("{} is already an admin", user.user.name), false).await?;
return Ok(());
}
let query = sqlx::query!("INSERT INTO admin_users (guild_id, user_id) VALUES ($1, $2);", ctx.guild_id().unwrap().get() as i64, user.user.id.get() as i64).execute(&ctx.data().pool).await;
match query {
Ok(_) => {
info!("Added user {} as admin in guild {}", user.user.name, ctx.guild_id().unwrap().get());
edit_response_message(msg, ctx, format!("Added {} as admin", user.user.name), false).await?;
edit_response_message(msg, ctx, &format!("Added {} as admin", user.user.name), false).await?;
}
Err(e) => {
error!("Failed to add user {} as admin in guild {}: {:?}", user.user.name, ctx.guild_id().unwrap().get(), e);
edit_response_message(msg, ctx, format!("Failed to add {} as admin: {}", user.user.name, e), false).await?;
edit_response_message(msg, ctx, &format!("Failed to add {} as admin: {}", user.user.name, e), false).await?;
}
}
Ok(())
@@ -131,18 +131,18 @@ async fn remove_admin_user<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>, u
trace!("Executing remove_admin_user for user {}", user.user.name);
if !is_target_admin_user(ctx, &user).await? {
trace!("User {} is not an admin", user.user.name);
edit_response_message(msg, ctx, format!("{} is not an admin", user.user.name), false).await?;
edit_response_message(msg, ctx, &format!("{} is not an admin", user.user.name), false).await?;
return Ok(());
}
let query = sqlx::query!("DELETE FROM admin_users WHERE guild_id = $1 AND user_id = $2;", ctx.guild_id().unwrap().get() as i64, user.user.id.get() as i64).execute(&ctx.data().pool).await;
match query {
Ok(_) => {
info!("Removed user {} as admin in guild {}", user.user.name, ctx.guild_id().unwrap().get());
edit_response_message(msg, ctx, format!("Removed {} as admin", user.user.name), false).await?;
edit_response_message(msg, ctx, &format!("Removed {} as admin", user.user.name), false).await?;
}
Err(e) => {
error!("Failed to remove user {} as admin in guild {}: {:?}", user.user.name, ctx.guild_id().unwrap().get(), e);
edit_response_message(msg, ctx, format!("Failed to remove {} as admin: {}", user.user.name, e), false).await?;
edit_response_message(msg, ctx, &format!("Failed to remove {} as admin: {}", user.user.name, e), false).await?;
}
}
Ok(())
@@ -151,7 +151,7 @@ async fn remove_admin_user<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>, u
async fn list_admin_users<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>) -> Result<(), Error> {
let admins = get_all_admin_users(ctx).await?;
let admin_strings: Vec<String> = admins.iter().map(|user| format!("<@{}>", user.id.get())).collect();
edit_response_message(msg, ctx, format!("Admin users: {}", admin_strings.join(", ")), true).await?;
edit_response_message(msg, ctx, &format!("Admin users: {}", admin_strings.join(", ")), true).await?;
Ok(())
}
@@ -159,18 +159,18 @@ async fn add_admin_role<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>, role
trace!("Executing add_admin_role for role {}", role.name);
if is_target_admin_role(ctx, &role).await? {
trace!("Role {} is already an admin role", role.name);
edit_response_message(msg, ctx, format!("{} is already an admin role", role.name), false).await?;
edit_response_message(msg, ctx, &format!("{} is already an admin role", role.name), false).await?;
return Ok(());
}
let query = sqlx::query!("INSERT INTO admin_roles (guild_id, role_id) VALUES ($1, $2);", ctx.guild_id().unwrap().get() as i64, role.id.get() as i64).execute(&ctx.data().pool).await;
match query {
Ok(_) => {
info!("Added role {} as admin role in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(msg, ctx, format!("Added {} as admin role", role.name), false).await?;
edit_response_message(msg, ctx, &format!("Added {} as admin role", role.name), false).await?;
}
Err(e) => {
error!("Failed to add role {} as admin role in guild {}: {:?}", role.name, ctx.guild_id().unwrap().get(), e);
edit_response_message(msg, ctx, format!("Failed to add {} as admin role: {}", role.name, e), false).await?;
edit_response_message(msg, ctx, &format!("Failed to add {} as admin role: {}", role.name, e), false).await?;
}
}
Ok(())
@@ -180,18 +180,18 @@ async fn remove_admin_role<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>, r
trace!("Executing remove_admin_role for role {}", role.name);
if !is_target_admin_role(ctx, &role).await? {
trace!("Role {} is not an admin role", role.name);
edit_response_message(msg, ctx, format!("{} is not an admin role", role.name), false).await?;
edit_response_message(msg, ctx, &format!("{} is not an admin role", role.name), false).await?;
return Ok(());
}
let query = sqlx::query!("DELETE FROM admin_roles WHERE guild_id = $1 AND role_id = $2;", ctx.guild_id().unwrap().get() as i64, role.id.get() as i64).execute(&ctx.data().pool).await;
match query {
Ok(_) => {
info!("Removed role {} as admin role in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(msg, ctx, format!("Removed {} as admin role", role.name), false).await?;
edit_response_message(msg, ctx, &format!("Removed {} as admin role", role.name), false).await?;
}
Err(e) => {
error!("Failed to remove role {} as admin role in guild {}: {:?}", role.name, ctx.guild_id().unwrap().get(), e);
edit_response_message(msg, ctx, format!("Failed to remove {} as admin role: {}", role.name, e), false).await?;
edit_response_message(msg, ctx, &format!("Failed to remove {} as admin role: {}", role.name, e), false).await?;
}
}
Ok(())
@@ -200,7 +200,7 @@ async fn remove_admin_role<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>, r
async fn list_admin_roles<'a>(ctx: Context<'a>, msg: &poise::ReplyHandle<'a>) -> Result<(), Error> {
let roles = sqlx::query!("SELECT role_id FROM admin_roles WHERE guild_id = $1", ctx.guild_id().unwrap().get() as i64).fetch_all(&ctx.data().pool).await?;
let role_strings: Vec<String> = roles.iter().map(|role| format!("<@&{}>", role.role_id)).collect();
edit_response_message(msg, ctx, format!("Admin roles: {}", role_strings.join(", ")), true).await?;
edit_response_message(msg, ctx, &format!("Admin roles: {}", role_strings.join(", ")), true).await?;
Ok(())
}
+20 -20
View File
@@ -29,7 +29,7 @@ pub async fn manage_auto_role(ctx: Context<'_>,
if !is_admin(ctx).await? {
debug!("User {} is not an admin, denying access", ctx.author().id.get());
edit_response_message(&response_message, ctx, "You are not allowed to run this command.".to_string(), false).await?;
edit_response_message(&response_message, ctx, "You are not allowed to run this command.", false).await?;
return Ok(());
}
@@ -38,28 +38,28 @@ pub async fn manage_auto_role(ctx: Context<'_>,
if let Some(s_role) = role {
enable_auto_role(ctx, &response_message, s_role).await?;
} else {
edit_response_message(&response_message, ctx, "Please provide a role".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide a role", false).await?;
}
}
Some(AutoRoleOperationType::SetAutoRole) => {
if let Some(s_role) = role {
set_auto_role(ctx, &response_message, s_role).await?;
} else {
edit_response_message(&response_message, ctx, "Please provide a role".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide a role", false).await?;
}
}
Some(AutoRoleOperationType::DisableAutoRole) => {
if let Some(s_role) = role {
disable_auto_role(ctx, &response_message, s_role).await?;
} else {
edit_response_message(&response_message, ctx, "Please provide a role".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide a role", false).await?;
}
}
Some(AutoRoleOperationType::ShowAutoRole) => {
show_auto_role(ctx, &response_message).await?;
}
None => {
edit_response_message(&response_message, ctx, "Please provide an operation".to_string(), false).await?;
edit_response_message(&response_message, ctx, "Please provide an operation", false).await?;
}
}
Ok(())
@@ -81,17 +81,17 @@ async fn enable_auto_role(ctx: Context<'_>, response_message: &poise::ReplyHandl
trace!("Executing enable_auto_role for role {}", role.name);
if !is_any_role_set_in_guild(ctx).await? {
trace!("No role is set in guild {}", ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "No role is set. Please set a role first.".to_string(), false).await?;
edit_response_message(response_message, ctx, "No role is set. Please set a role first.", false).await?;
return Ok(());
}
if !does_role_exist_in_guild(ctx, &role).await? {
trace!("Role {} does not exist in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "This role does not exist".to_string(), false).await?;
edit_response_message(response_message, ctx, "This role does not exist", false).await?;
return Ok(());
}
if is_auto_role_enabled(ctx, &role).await? {
trace!("Auto role {} is already enabled in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "Auto role is already enabled".to_string(), false).await?;
edit_response_message(response_message, ctx, "Auto role is already enabled", false).await?;
return Ok(());
}
@@ -99,11 +99,11 @@ async fn enable_auto_role(ctx: Context<'_>, response_message: &poise::ReplyHandl
match query {
Ok(_) => {
debug!("Enabled auto role {} in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, format!("Enabled auto role: <@&{}>", role.id.get()), false).await?;
edit_response_message(response_message, ctx, &format!("Enabled auto role: <@&{}>", role.id.get()), false).await?;
}
Err(e) => {
error!("Failed to enable {} as auto role in guild {}: {}", role.id.get(), ctx.guild_id().unwrap().get(), e);
edit_response_message(response_message, ctx, format!("Failed to enable {} as auto role: {}", role.id.get(), e), false).await?;
edit_response_message(response_message, ctx, &format!("Failed to enable {} as auto role: {}", role.id.get(), e), false).await?;
}
}
@@ -131,7 +131,7 @@ async fn set_auto_role(ctx: Context<'_>, response_message: &poise::ReplyHandle<'
trace!("Executing set_auto_role for role {}", role.name);
if !does_role_exist_in_guild(ctx, &role).await? {
trace!("Role {} does not exist in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "This role does not exist".to_string(), false).await?;
edit_response_message(response_message, ctx, "This role does not exist", false).await?;
return Ok(());
}
@@ -139,11 +139,11 @@ async fn set_auto_role(ctx: Context<'_>, response_message: &poise::ReplyHandle<'
match query {
Ok(_) => {
debug!("Set {} as auto role in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, format!("Set {} as auto role", role.name), false).await?;
edit_response_message(response_message, ctx, &format!("Set {} as auto role", role.name), false).await?;
}
Err(e) => {
error!("Failed to set {} as auto role in guild {}: {}", role.id.get(), ctx.guild_id().unwrap().get(), e);
edit_response_message(response_message, ctx, format!("Failed to set {} as auto role: {}", role.id.get(), e), false).await?;
edit_response_message(response_message, ctx, &format!("Failed to set {} as auto role: {}", role.id.get(), e), false).await?;
}
}
@@ -154,12 +154,12 @@ async fn disable_auto_role(ctx: Context<'_>, response_message: &poise::ReplyHand
trace!("Executing disable_auto_role for role {}", role.name);
if !is_any_role_set_in_guild(ctx).await? {
trace!("No role is set in guild {}", ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "No auto role is set. Please set a role first.".to_string(), false).await?;
edit_response_message(response_message, ctx, "No auto role is set. Please set a role first.", false).await?;
return Ok(());
}
if !is_auto_role_enabled(ctx, &role).await? {
trace!("Auto role {} is already disabled in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "Auto role is already disabled".to_string(), false).await?;
edit_response_message(response_message, ctx, "Auto role is already disabled", false).await?;
return Ok(());
}
@@ -167,11 +167,11 @@ async fn disable_auto_role(ctx: Context<'_>, response_message: &poise::ReplyHand
match query {
Ok(_) => {
debug!("Disabled auto role {} in guild {}", role.name, ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, format!("Disabled auto role: {}", role.name), false).await?;
edit_response_message(response_message, ctx, &format!("Disabled auto role: {}", role.name), false).await?;
}
Err(e) => {
error!("Failed to disable {} as auto role in guild {}: {}", role.id.get(), ctx.guild_id().unwrap().get(), e);
edit_response_message(response_message, ctx, format!("Failed to disable {} as auto role: {}", role.id.get(), e), false).await?;
edit_response_message(response_message, ctx, &format!("Failed to disable {} as auto role: {}", role.id.get(), e), false).await?;
}
}
@@ -198,16 +198,16 @@ async fn show_auto_role(ctx: Context<'_>, response_message: &poise::ReplyHandle<
trace!("Executing show_auto_role");
if !is_any_role_set_in_guild(ctx).await? {
trace!("No role is set in guild {}", ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "No role has been set yet.".to_string(), false).await?;
edit_response_message(response_message, ctx, "No role has been set yet.", false).await?;
return Ok(());
}
if !is_any_auto_role_enabled(ctx).await? {
trace!("Auto role is disabled in guild {}", ctx.guild_id().unwrap().get());
edit_response_message(response_message, ctx, "Auto role is disabled.".to_string(), false).await?;
edit_response_message(response_message, ctx, "Auto role is disabled.", false).await?;
return Ok(());
}
let role_id = sqlx::query!("SELECT role_id FROM auto_roles WHERE guild_id = $1", ctx.guild_id().unwrap().get() as i64).fetch_optional(&ctx.data().pool).await?;
edit_response_message(response_message, ctx, format!("Currently, <@&{}> is set as auto role and is enabled.", role_id.unwrap().role_id), false).await?;
edit_response_message(response_message, ctx, &format!("Currently, <@&{}> is set as auto role and is enabled.", role_id.unwrap().role_id), false).await?;
Ok(())
}
+1 -1
View File
@@ -17,7 +17,7 @@ pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
// We divide by 1000 since we get the time in microseconds, and then multiply by 2 to get the roundtrip time
// This is arguably not the best way to calculate ping, since it assumes perfect clock accuracy, but I'm lazy
let msg = format!("Current ping: {} ms", ping);
trace_message(msg.clone(), ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
trace_message(&msg, ctx.channel_id().to_string(), ctx.guild_id().unwrap().to_string()).await;
ctx.say(msg).await?;
debug!("Ping command performed for user {} with ping {}", ctx.author().name, ping);
Ok(())
+1 -1
View File
@@ -26,7 +26,7 @@ pub async fn speedy_pc(
let msg = format!("Running command: {:?}", command);
trace_message(
msg,
&msg,
ctx.channel_id().to_string(),
ctx.guild_id()
.map(|guild_id| guild_id.to_string())
+1 -1
View File
@@ -15,7 +15,7 @@ pub async fn version(ctx: Context<'_>) -> Result<(), Error> {
trace!("Loading embedded version information");
let msg = format!("Current version:\n{VERSION}");
trace_message(
msg.clone(),
&msg,
ctx.channel_id().to_string(),
ctx.guild_id().unwrap().to_string(),
)
+2 -2
View File
@@ -2,11 +2,11 @@ use crate::{Context, Error};
use poise::serenity_prelude as sere;
use tracing::trace;
pub async fn trace_message(msg: String, channel: String, guild: String) {
pub async fn trace_message(msg: &str, channel: String, guild: String) {
trace!("Saying \"{}\" in channel {} in guild {}", msg, channel, guild);
}
pub async fn edit_response_message<'a>(response_message: &poise::ReplyHandle<'a>, ctx: Context<'_>, content: String, silent: bool) -> Result<(), Error> {
pub async fn edit_response_message<'a>(response_message: &poise::ReplyHandle<'a>, ctx: Context<'_>, content: &str, silent: bool) -> Result<(), Error> {
if silent {
response_message.edit(ctx, poise::CreateReply::default().content(content).allowed_mentions(serenity::all::CreateAllowedMentions::new().empty_users())).await?;
} else {