Skip to content

Commit

Permalink
Merge pull request #21 from merlinfuchs/twilight-v12
Browse files Browse the repository at this point in the history
[WIP] Update twilight to >0.12
  • Loading branch information
merlinfuchs authored Aug 6, 2022
2 parents 09cd7b5 + f1377da commit f50b29b
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 290 deletions.
32 changes: 16 additions & 16 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ edition = "2021"
tokio = { version = "1.18.2", features = ["macros", "rt-multi-thread"] }
serde = { version = "1.0.137", features = ["derive"] }
actix-web = "4.0.1"
twilight-gateway = "0.11.0"
twilight-http = "0.11.0"
twilight-http-ratelimiting = "0.11.0"
twilight-model = "0.11.0"
twilight-cache-inmemory = { version = "0.11.0", features = ["permission-calculator"] }
twilight-util = { version = "0.11.0", features = ["builder", "permission-calculator"] }
twilight-gateway = "0.12"
twilight-http = "0.12"
twilight-http-ratelimiting = "0.12"
twilight-model = "0.12"
twilight-cache-inmemory = { version = "0.12", features = ["permission-calculator"] }
twilight-util = { version = "0.12", features = ["builder", "permission-calculator"] }
lazy_static = "1.4.0"
rust-embed = { version = "6.4.0", optional = true }
mime = "0.3.16"
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/wire/guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl From<&Role> for GuildRoleWire {
pub struct GuildChannelWire {
pub id: Id<ChannelMarker>,
pub name: Option<String>,
pub position: Option<i64>,
pub position: Option<i16>,
pub parent_id: Option<String>,
#[serde(rename = "type")]
pub kind: ChannelType,
Expand Down
87 changes: 48 additions & 39 deletions backend/src/bot/commands/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use twilight_http::client::InteractionClient;
use twilight_model::application::command::{Command, CommandType};
use twilight_model::application::component::text_input::TextInputStyle;
use twilight_model::application::component::{ActionRow, Component, TextInput};
use twilight_model::application::interaction::modal::ModalSubmitInteraction;
use twilight_model::application::interaction::ApplicationCommand;
use twilight_model::application::interaction::application_command::CommandData;
use twilight_model::application::interaction::modal::ModalInteractionData;
use twilight_model::application::interaction::Interaction;
use twilight_model::channel::ChannelType;
use twilight_model::guild::Permissions;
use twilight_model::http::interaction::{
Expand All @@ -19,8 +20,8 @@ use crate::bot::{get_bot_permissions_in_channel, DISCORD_CACHE, DISCORD_HTTP};

pub fn command_definition() -> Command {
CommandBuilder::new(
"embed".into(),
"Quickly create a simple embed message".into(),
"embed",
"Quickly create a simple embed message",
CommandType::ChatInput,
)
.dm_permission(true)
Expand All @@ -30,14 +31,15 @@ pub fn command_definition() -> Command {

pub async fn handle_command(
http: InteractionClient<'_>,
cmd: Box<ApplicationCommand>,
interaction: Interaction,
_cmd: Box<CommandData>,
) -> InteractionResult {
let user = cmd.member.unwrap().user.unwrap();
let user = interaction.member.unwrap().user.unwrap();
let avatar_url = user_avatar_url(user.id, user.discriminator, user.avatar, true);

http.create_response(
cmd.id,
&cmd.token,
interaction.id,
&interaction.token,
&InteractionResponse {
kind: InteractionResponseType::Modal,
data: Some(InteractionResponseData {
Expand Down Expand Up @@ -116,9 +118,10 @@ pub async fn handle_command(

pub async fn handle_modal(
http: InteractionClient<'_>,
modal: Box<ModalSubmitInteraction>,
interaction: Interaction,
modal: ModalInteractionData,
) -> InteractionResult {
if !modal
if !interaction
.member
.unwrap()
.permissions
Expand All @@ -127,20 +130,20 @@ pub async fn handle_modal(
{
simple_response(
&http,
modal.id,
&modal.token,
interaction.id,
&interaction.token,
"You need **Manage Webhook** permissions to use this command.".into(),
)
.await?;
return Err(InteractionError::NoOp);
}

let bot_perms = get_bot_permissions_in_channel(modal.channel_id);
let bot_perms = get_bot_permissions_in_channel(interaction.channel_id.unwrap());
if !bot_perms.contains(Permissions::MANAGE_WEBHOOKS) {
simple_response(
&http,
modal.id,
&modal.token,
interaction.id,
&interaction.token,
"The bot needs **Manage Webhook** permissions to create webhooks.".into(),
)
.await?;
Expand All @@ -153,24 +156,30 @@ pub async fn handle_modal(
let mut url = None;
let mut description = None;

for component in modal.data.components {
for component in modal.components {
for component in component.components {
match component.custom_id.as_str() {
"username" if component.value.len() != 0 => username = Some(component.value),
"avatar_url" if component.value.len() != 0 => avatar_url = Some(component.value),
"title" if component.value.len() != 0 => title = Some(component.value),
"url" if component.value.len() != 0 => url = Some(component.value),
"description" if component.value.len() != 0 => description = Some(component.value),
_ => {}
if let Some(value) = component.value {
match component.custom_id.as_str() {
"username" if value.len() != 0 => username = Some(value),
"avatar_url" if value.len() != 0 => {
avatar_url = Some(value)
}
"title" if value.len() != 0 => title = Some(value),
"url" if value.len() != 0 => url = Some(value),
"description" if value.len() != 0 => {
description = Some(value)
}
_ => {}
}
}
}
}

if title.is_none() && description.is_none() {
simple_response(
&http,
modal.id,
&modal.token,
interaction.id,
&interaction.token,
"You have to set either the embed title or the embed description.".into(),
)
.await?;
Expand All @@ -180,8 +189,8 @@ pub async fn handle_modal(
if title.is_none() && url.is_some() {
simple_response(
&http,
modal.id,
&modal.token,
interaction.id,
&interaction.token,
"An embed URL without an embed title isn't possible.".into(),
)
.await?;
Expand All @@ -199,21 +208,21 @@ pub async fn handle_modal(
embed = embed.description(description);
}

let channel = DISCORD_CACHE.channel(modal.channel_id).unwrap();
let channel = DISCORD_CACHE
.channel(interaction.channel_id.unwrap())
.unwrap();
let (channel_id, thread_id) = match channel.kind {
ChannelType::GuildPrivateThread
| ChannelType::GuildPublicThread
| ChannelType::GuildNewsThread => (channel.parent_id.unwrap(), Some(modal.channel_id)),
_ => (modal.channel_id, None),
| ChannelType::GuildNewsThread => (
channel.parent_id.unwrap(),
Some(interaction.channel_id.unwrap()),
),
_ => (interaction.channel_id.unwrap(), None),
};

let (webhook_id, webhook_token) = get_webhook_for_channel(
&http,
channel_id,
modal.id,
&modal.token,
)
.await?;
let (webhook_id, webhook_token) =
get_webhook_for_channel(&http, channel_id, interaction.id, &interaction.token).await?;

let mut exec_req = DISCORD_HTTP.execute_webhook(webhook_id, &webhook_token);
if let Some(thread_id) = thread_id {
Expand All @@ -235,8 +244,8 @@ pub async fn handle_modal(

simple_response(
&http,
modal.id,
&modal.token,
interaction.id,
&interaction.token,
"Your embed message has been created!".into(),
)
.await?;
Expand Down
Loading

0 comments on commit f50b29b

Please sign in to comment.