diff --git a/src/discord_gleam.gleam b/src/discord_gleam.gleam index 92c5e82..f6f4079 100644 --- a/src/discord_gleam.gleam +++ b/src/discord_gleam.gleam @@ -34,3 +34,12 @@ pub fn kick_member( ) -> #(String, String) { endpoints.kick_member(bot.token, guild_id, user_id, reason) } + +pub fn ban_member( + bot: bot.Bot, + guild_id: String, + user_id: String, + reason: String, +) -> #(String, String) { + endpoints.ban_member(bot.token, guild_id, user_id, reason) +} diff --git a/src/discord_gleam/http/endpoints.gleam b/src/discord_gleam/http/endpoints.gleam index abcdfaf..718849d 100644 --- a/src/discord_gleam/http/endpoints.gleam +++ b/src/discord_gleam/http/endpoints.gleam @@ -114,3 +114,41 @@ pub fn kick_member( } } } + +pub fn ban_member( + token: String, + guild_id: String, + user_id: String, + reason: String, +) -> #(String, String) { + let request = + request.new_auth_with_header( + http.Put, + "/guilds/" <> guild_id <> "/bans/" <> user_id, + token, + #("X-Audit-Log-Reason", reason), + ) + + case hackney.send(request) { + Ok(resp) -> { + case resp.status { + 204 -> { + logging.log(logging.Debug, "Banned member") + #("OK", resp.body) + } + _ -> { + logging.log(logging.Error, "Failed to ban member") + io.debug(resp.body) + + #("FAILED", resp.body) + } + } + } + Error(err) -> { + logging.log(logging.Error, "Failed to ban member: ") + io.debug(err) + + #("FAILED", "ERROR") + } + } +} diff --git a/src/discord_gleam/types/user.gleam b/src/discord_gleam/types/user.gleam index 8aaf361..d8728e0 100644 --- a/src/discord_gleam/types/user.gleam +++ b/src/discord_gleam/types/user.gleam @@ -2,11 +2,10 @@ import discord_gleam/discord/snowflake.{type Snowflake} import discord_gleam/internal/error import gleam/dynamic import gleam/json +import gleam/option.{type Option} import gleam/result import gleam/string -import gleam/option.{type Option} - /// User object containing PartialUser and FullUser pub type User { PartialUser( diff --git a/test/example_bot.gleam b/test/example_bot.gleam index 86086e3..0ad4cbc 100644 --- a/test/example_bot.gleam +++ b/test/example_bot.gleam @@ -1,7 +1,6 @@ import discord_gleam import discord_gleam/event_handler import discord_gleam/types/message -import gleam/io import gleam/list import gleam/string import logging @@ -91,6 +90,54 @@ fn event_handler(bot, packet: event_handler.Packet) { } False -> Nil } + case string.starts_with(message.d.content, "!ban ") { + True -> { + let args = string.split(message.d.content, " ") + + let args = case list.pop(args, fn(x) { x == "!ban" }) { + Ok(args) -> args.1 + Error(_) -> [""] + } + + let user = case list.first(args) { + Ok(x) -> x + Error(_) -> "" + } + + let args = case list.pop(args, fn(x) { x == user }) { + Ok(args) -> args.1 + Error(_) -> [""] + } + + let user = string.replace(user, "<@", "") + let user = string.replace(user, ">", "") + + let reason = string.join(args, " ") + + let resp = + discord_gleam.ban_member(bot, message.d.guild_id, user, reason) + + case resp.0 { + "OK" -> { + discord_gleam.send_message( + bot, + message.d.channel_id, + "Banned user!", + [], + ) + } + _ -> { + discord_gleam.send_message( + bot, + message.d.channel_id, + "Failed to ban user!", + [], + ) + } + } + } + False -> Nil + } } _ -> Nil }