From 867a744720c46c0b04a2d34c2119ad366aa440ef Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Tue, 30 Oct 2018 15:10:10 +0100 Subject: Add Function to neutralise Mentions (#414) --- examples/05_command_framework/src/main.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'examples/05_command_framework/src') diff --git a/examples/05_command_framework/src/main.rs b/examples/05_command_framework/src/main.rs index e69934c..843fb43 100644 --- a/examples/05_command_framework/src/main.rs +++ b/examples/05_command_framework/src/main.rs @@ -20,6 +20,7 @@ use serenity::model::gateway::Ready; use serenity::model::Permissions; use serenity::prelude::Mutex; use serenity::prelude::*; +use serenity::utils::{content_safe, ContentSafeOptions}; use std::collections::HashMap; use std::env; use std::fmt::Write; @@ -175,6 +176,10 @@ fn main() { // Make this command use the "complicated" bucket. .bucket("complicated") .cmd(commands)) + // Command that will repeat passed arguments and remove user and + // role mentions with safe alternative. + .command("say", |c| c + .cmd(say)) .group("Emoji", |g| g // Sets multiple prefixes for a group. // This requires us to call commands in this group @@ -250,6 +255,32 @@ command!(commands(ctx, msg, _args) { } }); +// Repeats what the user passed as argument but ensures that user and role +// mentions are replaced with a safe textual alternative. +// In this example channel mentions are excluded via the `ContentSafeOptions`. +command!(say(_ctx, msg, args) { + let mut settings = if let Some(guild_id) = msg.guild_id { + // By default roles, users, and channel mentions are cleaned. + ContentSafeOptions::default() + // We do not want to clean channal mentions as they + // do not ping users. + .clean_channel(false) + // If it's a guild channel, we want mentioned users to be displayed + // as their display name. + .display_as_member_from(guild_id) + } else { + ContentSafeOptions::default() + .clean_channel(false) + .clean_role(false) + }; + + let mut content = content_safe(&args.full(), &settings); + + if let Err(why) = msg.channel_id.say(&content) { + println!("Error sending message: {:?}", why); + } +}); + // A function which acts as a "check", to determine whether to call a command. // // In this case, this command checks to ensure you are the owner of the message -- cgit v1.2.3