Skip to content

Commit

Permalink
Rewrite with poise
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz committed Apr 23, 2024
1 parent bd73251 commit 3b3a84b
Show file tree
Hide file tree
Showing 10 changed files with 657 additions and 290 deletions.
579 changes: 502 additions & 77 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@ semver = "1.0.22"
iso8601 = "0.6.1"
shuttle-serenity = "0.43.0"
shuttle-runtime = "0.43.0"

[dependencies.serenity]
version = "0.12.1"
features = ["collector"]
poise = "0.6.1"
reqwest = "0.12.4"
11 changes: 4 additions & 7 deletions src/commands/discord.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use crate::{Context, Error};

#[command]
async fn discord(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(&ctx, "https://discord.gg/CsqAfs9CnM").await?;
#[poise::command(prefix_command, slash_command)]
pub async fn discord(ctx: Context<'_>) -> Result<(), Error> {
ctx.reply("https://discord.gg/CsqAfs9CnM").await?;
Ok(())
}
37 changes: 4 additions & 33 deletions src/commands/does_my_game_work.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
/*
created by hahayupgit 2024/4/16
desc: command for if someone asks "does x game work?"
*/
use serenity::all::EditMessage;
use serenity::futures::StreamExt;
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use std::time::Duration;

#[command]
#[aliases("dmgw")]
async fn does_my_game_work(ctx: &Context, msg: &Message) -> CommandResult {
use crate::{Context, Error};

#[poise::command(prefix_command, slash_command, aliases("dmgw"))]
pub async fn does_my_game_work(ctx: Context<'_>) -> Result<(), Error> {
let reply = String::from("Does your game work? Check https://docs.getwhisky.app/game-support/ first, and search to see if anyone else has this same issue in this Discord or the GitHub repository.
Can't find your game from those sources? Check here:
Expand All @@ -27,23 +15,6 @@ If you've checked all of these places and still can't find an answer, feel free
As a reminder: Most games with EasyAntiCheat and most other anti-cheats will **NOT** work without workarounds.");

let mut reply_msg = msg.reply(&ctx, reply).await?;

// below code pulled directly from serenity-rs documentation
// can be found here: https://docs.rs/serenity/latest/serenity/builder/struct.EditMessage.html#method.suppress_embeds

// When the embed appears, a MessageUpdate event is sent and we suppress
// the embed. No MessageUpdate event is sent if the message contains no
// embeddable link or if the link has been posted before and is still
// cached in Discord's servers (in which case the embed appears
// immediately), no MessageUpdate event is sent. To not wait forever in
// those cases, a timeout of 2000ms was added.
let msg_id = reply_msg.id;
let mut message_updates = serenity::collector::collect(&ctx.shard, move |ev| match ev {
Event::MessageUpdate(x) if x.id == msg_id => Some(()),
_ => None,
});
let _ = tokio::time::timeout(Duration::from_millis(2000), message_updates.next()).await;
reply_msg.edit(&ctx, EditMessage::new().suppress_embeds(true)).await?;
ctx.reply(reply).await?;
Ok(())
}
64 changes: 18 additions & 46 deletions src/commands/game_support.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,28 @@
/*
modified by hahayupgit 2024/4/9
desc: added ability to return webpage of inputted argument
*/

use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;

#[command]
#[aliases("gs")]
async fn game_support(ctx: &Context, msg: &Message) -> CommandResult {
use reqwest::StatusCode;
use crate::{Context, Error};

#[poise::command(prefix_command, slash_command, aliases("gs"))]
pub async fn game_support(ctx: Context<'_>,
#[description = "Game Name"] game_name: Option<String>) -> Result<(), Error> {
// String containing default response
let mut site = String::from("https://docs.getwhisky.app/game-support/");

// command stripped of leading and trailing whitespace
let mut message = msg.content.trim().to_string();
match game_name {
Some(name) => {
site.push_str(&name);

// if command is only "~gs" or "~game_support", reply with default response
if message == "~gs" || message == "~game_support" {
msg.reply(&ctx, site).await?;
}
let resp = reqwest::get(site.clone()).await?;

// if command has arguments
else {

// determine which command was said & remove command given
if message.contains("~gs") {
message = message.replace("~gs ", "");
}
else {
message = message.replace("~game_support ", "");
if resp.status() != StatusCode::NOT_FOUND {
ctx.reply(site).await?;
} else {
ctx.reply("Hmm, seems that game isn't in our docs.").await?;
}
},
None => {
ctx.reply(site).await?;
}

// concatenate the argument to the original default response & reply
site.push_str(&message);
msg.reply(&ctx, site).await?;

/*
NOTE: this method does not have any checks for
URLs that are incorrect. this should not pose
any security threats, however may make it harder
for people who don't know the exact URL of longer games
(i.e. Star Wars Jedi: Fallen Order being sw-fallen-order
or Geometry Wars 3: Dimensions Evolved being gw3-dimensions-evolved).
if i was a better developer, i'd figure out how to handle
this. but i'm not, so i may return to this in a future change.
- hahayupgit 2024/4/9
*/
}

Ok(())
}
11 changes: 4 additions & 7 deletions src/commands/github.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use crate::{Context, Error};

#[command]
async fn github(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(&ctx, "https://github.com/Whisky-App/Whisky").await?;
#[poise::command(prefix_command, slash_command)]
pub async fn github(ctx: Context<'_>) -> Result<(), Error> {
ctx.reply("https://github.com/Whisky-App/Whisky").await?;
Ok(())
}
11 changes: 4 additions & 7 deletions src/commands/ping.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use crate::{Context, Error};

#[command]
async fn ping(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(&ctx, "Pong! ~").await?;
#[poise::command(prefix_command, slash_command)]
pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
ctx.reply("Pong! ~").await?;
Ok(())
}

17 changes: 6 additions & 11 deletions src/commands/say.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use crate::{Context, Error};

#[command]
#[owners_only]
#[only_in("guild")]
async fn say(ctx: &Context, msg: &Message) -> CommandResult {
let message = msg.content.replace("~say ", "");
msg.channel_id.say(&ctx, message).await?;
msg.delete(&ctx).await?;
#[poise::command(prefix_command, slash_command, owners_only, guild_only)]
pub async fn say(ctx: Context<'_>,
#[description = "Message"] message: String) -> Result<(), Error> {
ctx.reply()
ctx.say(message).await?;

Ok(())
}
Expand Down
11 changes: 4 additions & 7 deletions src/commands/website.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use serenity::framework::standard::macros::command;
use serenity::framework::standard::CommandResult;
use serenity::model::prelude::*;
use serenity::prelude::*;
use crate::{Context, Error};

#[command]
async fn website(ctx: &Context, msg: &Message) -> CommandResult {
msg.reply(&ctx, "https://getwhisky.app/").await?;
#[poise::command(prefix_command, slash_command)]
pub async fn website(ctx: Context<'_>) -> Result<(), Error> {
ctx.reply("https://getwhisky.app/").await?;
Ok(())
}
Loading

0 comments on commit 3b3a84b

Please sign in to comment.