Skip to content

Commit

Permalink
Update Twilight to 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Cldfire committed Mar 21, 2022
1 parent b1c2c2c commit 0bbc9fd
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 69 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Notable `mc-server-wrapper` changes, tracked in the [keep a changelog](https://k
### Internal

* Added dependency on `time` 0.3
* Updated `twilight` to 0.9
* Updated `twilight` to 0.10
* Updated `tui` to 0.17
* Updated `crostterm` to 0.23.1
* Updated `textwrap` to 0.15
Expand Down
73 changes: 38 additions & 35 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ discord = "Info"
### Discord bridge setup

* Register an application and a bot with [Discord](https://discordapp.com/developers/applications)
* Toggle `Server Members Intent` to on under `Privileged Gateway Intents`
* This is used to receive member updates from your guild (such as when someone changes their nickname so we can change the name we display in-game)
* Enable some things in the `Privileged Gateway Intents` section of the bot's admin portal
* Toggle `Server Members Intent` on
* This is used to receive member updates from your guild (such as when someone changes their nickname so we can change the name we display in-game)
* Toggle `Message Content Intent` on
* This is used to receive messsage content from the channel for the chat bridge in Discord so we can relay chat there into Minecraft
* Add the bot to the guild you want to bridge to
* Get the ID of the channel you want to bridge to (Google this for instructions)
* Provide the bot token and channel ID in the config file
Expand Down
10 changes: 5 additions & 5 deletions mc-server-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ mc-server-wrapper-lib = { path = "../mc-server-wrapper-lib" }
structopt = "0.3"
tokio = { version = "1.15.0", features = ["full"] }
futures = "0.3"
twilight-http = "0.9"
twilight-gateway = "0.9"
twilight-cache-inmemory = "0.9"
twilight-model = "0.9"
twilight-http = "0.10.1"
twilight-gateway = "0.10.1"
twilight-cache-inmemory = "0.10.1"
twilight-model = "0.10.1"
twilight-command-parser = "0.9"
twilight-mention = "0.9"
twilight-mention = "0.10"
# TODO: replace this with a crate of my own
minecraft-chat = "0.1"
log = "0.4"
Expand Down
11 changes: 7 additions & 4 deletions mc-server-wrapper/src/discord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ impl DiscordBridge {
) -> Result<(Self, Events), anyhow::Error> {
let (cluster, events) = Cluster::builder(
token.clone(),
Intents::GUILDS | Intents::GUILD_MESSAGES | Intents::GUILD_MEMBERS,
Intents::GUILDS
| Intents::GUILD_MESSAGES
| Intents::GUILD_MEMBERS
| Intents::MESSAGE_CONTENT,
)
.build()
.await
Expand Down Expand Up @@ -203,8 +206,8 @@ impl DiscordBridge {
if let Some(channel_name) = guild
.channels
.iter()
.find(|c| c.id() == self.bridge_channel_id)
.map(|c| c.name())
.find(|c| c.id == self.bridge_channel_id)
.and_then(|c| c.name.as_ref())
{
info!(
"Connected to guild '{}', bridging chat to '#{}'",
Expand Down Expand Up @@ -350,7 +353,7 @@ impl DiscordBridge {
mention.id,
cmm.as_ref()
.and_then(|cm| cm.nick())
.unwrap_or_else(|| mention.name.as_str()),
.unwrap_or(mention.name.as_str()),
);
}

Expand Down
56 changes: 37 additions & 19 deletions mc-server-wrapper/src/discord/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ pub fn format_mentions_in<S: AsRef<str>>(
MessageSpan::Mention(mention_type, raw) => match mention_type {
MentionType::Channel(id) => {
let cow = cache
.guild_channel(id)
.map(|channel| Cow::from(format!("#{}", channel.name())))
.channel(id)
.and_then(|channel| {
channel
.name
.as_ref()
.map(|channel_name| Cow::from(format!("#{}", channel_name)))
})
// Throughout this function we fallback to the raw, unformatted
// text if we're unable to fetch relevant info from the cache
.unwrap_or_else(|| Cow::from(raw));
Expand Down Expand Up @@ -316,30 +321,43 @@ mod sanitize_for_markdown {
mod content_format_mentions {
use twilight_gateway::Event;
use twilight_model::{
channel::{Channel, ChannelType, GuildChannel, TextChannel},
channel::{Channel, ChannelType},
gateway::payload,
guild::{Permissions, Role},
};

use super::*;

fn make_text_channel() -> Event {
Event::ChannelCreate(payload::incoming::ChannelCreate(Channel::Guild(
GuildChannel::Text(TextChannel {
id: Id::new(1234),
guild_id: Some(Id::new(1)),
kind: ChannelType::GuildText,
last_message_id: None,
last_pin_timestamp: None,
name: "test-channel".into(),
nsfw: false,
permission_overwrites: vec![],
parent_id: None,
position: 0,
rate_limit_per_user: None,
topic: Some("a test channel".into()),
}),
)))
Event::ChannelCreate(Box::new(payload::incoming::ChannelCreate(Channel {
application_id: None,
bitrate: None,
default_auto_archive_duration: None,
guild_id: Some(Id::new(1)),
icon: None,
id: Id::new(1234),
invitable: None,
kind: ChannelType::GuildText,
last_message_id: None,
last_pin_timestamp: None,
member: None,
member_count: None,
message_count: None,
name: Some("test-channel".into()),
newly_created: None,
nsfw: None,
owner_id: None,
parent_id: None,
permission_overwrites: None,
position: None,
rate_limit_per_user: None,
recipients: None,
rtc_region: None,
thread_metadata: None,
topic: None,
user_limit: None,
video_quality_mode: None,
})))
}

fn make_role() -> Event {
Expand Down
5 changes: 2 additions & 3 deletions mc-server-wrapper/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl LogsState {
.iter_mut()
// Only wrap the records we could potentially be displaying
.skip(num_records.saturating_sub(available_lines))
.map(|r| {
.flat_map(|r| {
// See if we can use a cached wrapped line
if let Some(wrapped) = &r.1 {
if wrapped.1 as usize == logs_area_width {
Expand All @@ -239,8 +239,7 @@ impl LogsState {

wrapped_lines_len += r.1.as_ref().unwrap().0.len();
r.1.as_ref().unwrap().0.clone()
})
.flatten(),
}),
);

if self.progress_bar.is_some() {
Expand Down

0 comments on commit 0bbc9fd

Please sign in to comment.