Split longer messages

This commit is contained in:
kageru 2021-05-04 13:23:12 +02:00
parent e0015323a0
commit 4eaae45f5f
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 19 additions and 4 deletions

View File

@ -1,3 +1,3 @@
# DIDgeridoo
A bot for people with dissociative identity disorder.
Currently only does and logs name changes.
Currently only does and logs name changes and has basic inbox functionality for a single user per instance.

View File

@ -106,7 +106,7 @@ async fn name(ctx: &Context, msg: &Message) -> CommandResult {
#[command]
async fn inbox(ctx: &Context, msg: &Message) -> CommandResult {
if msg.author.id != INBOX_OWNER {
if msg.author.id != INBOX_OWNER || !msg.is_private() {
send_or_log!(
msg.reply(&ctx, "You don’t have the permission to do that")
.await
@ -122,8 +122,23 @@ async fn inbox(ctx: &Context, msg: &Message) -> CommandResult {
);
return Ok(());
}
let output = messages.into_iter().join("\n");
send_or_log!(msg.reply(&ctx, output).await);
for message in &messages {
let content = message.to_string();
// Since we’re prepending a name (and later a time) to the message,
// it could go over the limit of 2000 characters.
// There is some leniency in the numbers here because unicode codepoints are meh.
if content.len() > 1900 {
let (first, second) = content.split_at(1500);
send_or_log!(msg.channel_id.say(&ctx, &first).await);
send_or_log!(msg.channel_id.say(&ctx, &second).await);
} else {
send_or_log!(msg.channel_id.say(&ctx, &content).await);
}
}
println!("{:?}", messages); // paranoia for now so the messages aren’t lost
if let Err(e) = INBOX.clear_messages(name) {
eprintln!("Could not clear inbox of user {} because {}", name, e);
}
} else {
send_or_log!(msg.reply(&ctx, "Kein Name für die Abfrage.").await);
}