Skip to content

Commit

Permalink
banning
Browse files Browse the repository at this point in the history
  • Loading branch information
cyteon committed Jul 18, 2024
1 parent 3bb3e96 commit 6744423
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/discord_gleam.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
38 changes: 38 additions & 0 deletions src/discord_gleam/http/endpoints.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
3 changes: 1 addition & 2 deletions src/discord_gleam/types/user.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
49 changes: 48 additions & 1 deletion test/example_bot.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 6744423

Please sign in to comment.