Skip to content

Commit

Permalink
Use the bitflags! macro instead of the undocumented macro (serenity…
Browse files Browse the repository at this point in the history
…-rs#1662)

The use of the `__impl_bitflags` macro was introduced at the time to help
language servers to resolve types for autocompletion. Much has changed
since then, `RLS` has been superseded by `rust-analyzer` and
`rust-analyzer` supports `local_inner_macros`.

BREAKING CHANGE: The `bits` field of the bitflags is now private. Use
`T::bits()` or `T::from_bits_truncate()` instead.
  • Loading branch information
nickelc authored and arqunis committed Mar 1, 2022
1 parent b30084f commit 4fcd7ef
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 198 deletions.
4 changes: 2 additions & 2 deletions command_attr/src/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ impl ToTokens for Permissions {
fn to_tokens(&self, stream: &mut TokenStream2) {
let bits = self.0;

let path = quote!(serenity::model::permissions::Permissions);
let path = quote!(serenity::model::permissions::Permissions::from_bits_truncate);

stream.extend(quote! {
#path { bits: #bits }
#path(#bits)
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/builder/create_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl<'a> CreateMessage<'a> {

/// Sets the flags for the message.
pub fn flags(&mut self, flags: MessageFlags) -> &mut Self {
self.0.insert("flags", from_number(flags.bits));
self.0.insert("flags", from_number(flags.bits()));
self
}

Expand Down
2 changes: 1 addition & 1 deletion src/builder/edit_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<'a> EditMessage<'a> {

/// Sets the flags for the message.
pub fn flags(&mut self, flags: MessageFlags) -> &mut Self {
self.0.insert("flags", from_number(flags.bits));
self.0.insert("flags", from_number(flags.bits()));
self
}

Expand Down
2 changes: 1 addition & 1 deletion src/builder/execute_webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl<'a> ExecuteWebhook<'a> {
/// # }
/// ```
pub fn flags(&mut self, flags: MessageFlags) -> &mut Self {
self.0.insert("flags", from_number(flags.bits));
self.0.insert("flags", from_number(flags.bits()));
self
}
}
Expand Down
29 changes: 12 additions & 17 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt::Display;
#[cfg(all(feature = "cache", feature = "model"))]
use std::fmt::Write;

use bitflags::__impl_bitflags;
use bitflags::bitflags;
use chrono::{DateTime, Utc};
#[cfg(feature = "simd-json")]
use simd_json::ValueAccess;
Expand Down Expand Up @@ -1230,30 +1230,25 @@ pub struct ChannelMention {
pub name: String,
}

/// Describes extra features of the message.
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
pub struct MessageFlags {
pub bits: u64,
}

__impl_bitflags! {
MessageFlags: u64 {
bitflags! {
/// Describes extra features of the message.
pub struct MessageFlags: u64 {
/// This message has been published to subscribed channels (via Channel Following).
CROSSPOSTED = 1 << 0;
const CROSSPOSTED = 1 << 0;
/// This message originated from a message in another channel (via Channel Following).
IS_CROSSPOST = 1 << 1;
const IS_CROSSPOST = 1 << 1;
/// Do not include any embeds when serializing this message.
SUPPRESS_EMBEDS = 1 << 2;
const SUPPRESS_EMBEDS = 1 << 2;
/// The source message for this crosspost has been deleted (via Channel Following).
SOURCE_MESSAGE_DELETED = 1 << 3;
const SOURCE_MESSAGE_DELETED = 1 << 3;
/// This message came from the urgent message system.
URGENT = 1 << 4;
const URGENT = 1 << 4;
/// This message has an associated thread, with the same id as the message.
HAS_THREAD = 1 << 5;
const HAS_THREAD = 1 << 5;
/// This message is only visible to the user who invoked the Interaction.
EPHEMERAL = 1 << 6;
const EPHEMERAL = 1 << 6;
/// This message is an Interaction Response and the bot is "thinking".
LOADING = 1 << 7;
const LOADING = 1 << 7;
}
}

Expand Down
99 changes: 43 additions & 56 deletions src/model/gateway.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! Models pertaining to the gateway.

// TODO: Remove after `GuildStatus` was replaced
#![allow(deprecated)]

use bitflags::{__impl_bitflags, bitflags};
use bitflags::bitflags;
use url::Url;

use super::prelude::*;
Expand Down Expand Up @@ -529,43 +526,33 @@ pub struct ActivityTimestamps {
pub start: Option<u64>,
}

/// [Gateway Intents] will limit the events your bot will receive via the gateway.
/// By default, all intents except [Privileged Intents] are selected.
///
/// # What are Intents
///
/// A [gateway intent] sets the types of gateway events
/// (e.g. member joins, guild integrations, guild emoji updates, ...) the
/// bot shall receive. Carefully picking the needed intents greatly helps
/// the bot to scale, as less intents will result in less events to be
/// received via the network from Discord and less processing needed for
/// handling the data.
///
/// # Privileged Intents
///
/// The intents [`GatewayIntents::GUILD_PRESENCES`] and [`GatewayIntents::GUILD_MEMBERS`]
/// are [Privileged Intents]. They need to be enabled in the
/// *developer portal*.
///
/// **Note**:
/// Once the bot is in 100 guilds or more, [the bot must be verified] in
/// order to use privileged intents.
///
/// [gateway intent]: https://discord.com/developers/docs/topics/gateway#privileged-intents
/// [Privileged Intents]: https://discord.com/developers/docs/topics/gateway#privileged-intents
/// [the bot must be verified]: https://support.discord.com/hc/en-us/articles/360040720412-Bot-Verification-and-Data-Whitelisting
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
pub struct GatewayIntents {
/// The flags composing gateway intents.
///
/// # Note
/// Do not modify this yourself; use the provided methods.
/// Do the same when creating, unless you're absolutely certain that you're giving valid intents flags.
pub bits: u64,
}

__impl_bitflags! {
GatewayIntents: u64 {
bitflags! {
/// [Gateway Intents] will limit the events your bot will receive via the gateway.
/// By default, all intents except [Privileged Intents] are selected.
///
/// # What are Intents
///
/// A [gateway intent] sets the types of gateway events
/// (e.g. member joins, guild integrations, guild emoji updates, ...) the
/// bot shall receive. Carefully picking the needed intents greatly helps
/// the bot to scale, as less intents will result in less events to be
/// received via the network from Discord and less processing needed for
/// handling the data.
///
/// # Privileged Intents
///
/// The intents [`GatewayIntents::GUILD_PRESENCES`] and [`GatewayIntents::GUILD_MEMBERS`]
/// are [Privileged Intents]. They need to be enabled in the
/// *developer portal*.
///
/// **Note**:
/// Once the bot is in 100 guilds or more, [the bot must be verified] in
/// order to use privileged intents.
///
/// [gateway intent]: https://discord.com/developers/docs/topics/gateway#privileged-intents
/// [Privileged Intents]: https://discord.com/developers/docs/topics/gateway#privileged-intents
/// [the bot must be verified]: https://support.discord.com/hc/en-us/articles/360040720412-Bot-Verification-and-Data-Whitelisting
pub struct GatewayIntents: u64 {
/// Enables following gateway events:
///
/// - GUILD_CREATE
Expand All @@ -586,7 +573,7 @@ __impl_bitflags! {
/// - STAGE_INSTANCE_CREATE
/// - STAGE_INSTANCE_UPDATE
/// - STAGE_INSTANCE_DELETE
GUILDS = 1;
const GUILDS = 1;
/// Enables following gateway events:
///
/// - GUILD_MEMBER_ADD
Expand All @@ -600,37 +587,37 @@ __impl_bitflags! {
/// Developer Portal and enable the toggle for *Privileged Intents*.
///
/// This intent is also necessary to even receive the events in contains.
GUILD_MEMBERS = 1 << 1;
const GUILD_MEMBERS = 1 << 1;
/// Enables following gateway events:
///
/// - GUILD_BAN_ADD
/// - GUILD_BAN_REMOVE
GUILD_BANS = 1 << 2;
const GUILD_BANS = 1 << 2;
/// Enables following gateway event:
///
/// - GUILD_EMOJIS_UPDATE
/// - GUILD_STICKERS_UPDATE
GUILD_EMOJIS_AND_STICKERS = 1 << 3;
const GUILD_EMOJIS_AND_STICKERS = 1 << 3;
/// Enables following gateway event:
///
/// - GUILD_INTEGRATIONS_UPDATE
/// - INTEGRATION_CREATE
/// - INTEGRATION_UPDATE
/// - INTEGRATION_DELETE
GUILD_INTEGRATIONS = 1 << 4;
const GUILD_INTEGRATIONS = 1 << 4;
/// Enables following gateway event:
///
/// - WEBHOOKS_UPDATE
GUILD_WEBHOOKS = 1 << 5;
const GUILD_WEBHOOKS = 1 << 5;
/// Enables following gateway events:
///
/// - INVITE_CREATE
/// - INVITE_DELETE
GUILD_INVITES = 1 << 6;
const GUILD_INVITES = 1 << 6;
/// Enables following gateway event:
///
/// - VOICE_STATE_UPDATE
GUILD_VOICE_STATES = 1 << 7;
const GUILD_VOICE_STATES = 1 << 7;
/// Enables following gateway event:
///
/// - PRESENCE_UPDATE
Expand All @@ -641,43 +628,43 @@ __impl_bitflags! {
/// Developer Portal and enable the toggle for *Privileged Intents*.
///
/// This intent is also necessary to even receive the events in contains.
GUILD_PRESENCES = 1 << 8;
const GUILD_PRESENCES = 1 << 8;
/// Enables following gateway events:
///
/// - MESSAGE_CREATE
/// - MESSAGE_UPDATE
/// - MESSAGE_DELETE
/// - MESSAGE_DELETE_BULK
GUILD_MESSAGES = 1 << 9;
const GUILD_MESSAGES = 1 << 9;
/// Enables following gateway events:
///
/// - MESSAGE_REACTION_ADD
/// - MESSAGE_REACTION_REMOVE
/// - MESSAGE_REACTION_REMOVE_ALL
/// - MESSAGE_REACTION_REMOVE_EMOJI
GUILD_MESSAGE_REACTIONS = 1 << 10;
const GUILD_MESSAGE_REACTIONS = 1 << 10;
/// Enable following gateway event:
///
/// - TYPING_START
GUILD_MESSAGE_TYPING = 1 << 11;
const GUILD_MESSAGE_TYPING = 1 << 11;
/// Enable following gateway events:
///
/// - MESSAGE_CREATE
/// - MESSAGE_UPDATE
/// - MESSAGE_DELETE
/// - CHANNEL_PINS_UPDATE
DIRECT_MESSAGES = 1 << 12;
const DIRECT_MESSAGES = 1 << 12;
/// Enable following gateway events:
///
/// - MESSAGE_REACTION_ADD
/// - MESSAGE_REACTION_REMOVE
/// - MESSAGE_REACTION_REMOVE_ALL
/// - MESSAGE_REACTION_REMOVE_EMOJI
DIRECT_MESSAGE_REACTIONS = 1 << 13;
const DIRECT_MESSAGE_REACTIONS = 1 << 13;
/// Enable following gateway event:
///
/// - TYPING_START
DIRECT_MESSAGE_TYPING = 1 << 14;
const DIRECT_MESSAGE_TYPING = 1 << 14;
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/model/guild/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::borrow::Cow;
use std::cmp::Reverse;
use std::fmt::{Display, Formatter, Result as FmtResult};

use bitflags::__impl_bitflags;
use bitflags::bitflags;
use chrono::{DateTime, Utc};

#[cfg(feature = "model")]
Expand Down Expand Up @@ -717,16 +717,11 @@ pub struct ThreadMember {
pub flags: ThreadMemberFlags,
}

/// Describes extra features of the message.
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
pub struct ThreadMemberFlags {
pub bits: u64,
}

__impl_bitflags! {
ThreadMemberFlags: u64 {
bitflags! {
/// Describes extra features of the message.
pub struct ThreadMemberFlags: u64 {
// Not documented.
NOTIFICATIONS = 1 << 0;
const NOTIFICATIONS = 1 << 0;
}
}

Expand Down
22 changes: 9 additions & 13 deletions src/model/guild/system_channel.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
use bitflags::__impl_bitflags;
use bitflags::bitflags;

/// Describes a system channel flags.
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash, Default)]
pub struct SystemChannelFlags {
pub bits: u64,
}

__impl_bitflags! {
SystemChannelFlags: u64 {
bitflags! {
/// Describes a system channel flags.
#[derive(Default)]
pub struct SystemChannelFlags: u64 {
/// Suppress member join notifications.
SUPPRESS_JOIN_NOTIFICATIONS = 1 << 0;
const SUPPRESS_JOIN_NOTIFICATIONS = 1 << 0;
/// Suppress server boost notifications.
SUPPRESS_PREMIUM_SUBSCRIPTIONS = 1 << 1;
const SUPPRESS_PREMIUM_SUBSCRIPTIONS = 1 << 1;
/// Suppress server setup tips.
SUPPRESS_GUILD_REMINDER_NOTIFICATIONS = 1 << 2;
const SUPPRESS_GUILD_REMINDER_NOTIFICATIONS = 1 << 2;
/// Hide member join sticker reply buttons.
SUPPRESS_JOIN_NOTIFICATION_REPLIES = 1 << 3;
const SUPPRESS_JOIN_NOTIFICATION_REPLIES = 1 << 3;
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/model/interactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod ping;

use application_command::ApplicationCommandInteraction;
use autocomplete::AutocompleteInteraction;
use bitflags::__impl_bitflags;
use bitflags::bitflags;
use message_component::MessageComponentInteraction;
use modal::ModalSubmitInteraction;
use ping::PingInteraction;
Expand Down Expand Up @@ -192,18 +192,12 @@ enum_number!(InteractionType {
ModalSubmit
});

/// The flags for an interaction response.
#[derive(Clone)]
#[non_exhaustive]
pub struct InteractionApplicationCommandCallbackDataFlags {
bits: u64,
}

__impl_bitflags! {
InteractionApplicationCommandCallbackDataFlags: u64 {
bitflags! {
/// The flags for an interaction response.
pub struct InteractionApplicationCommandCallbackDataFlags: u64 {
/// Interaction message will only be visible to sender and will
/// be quickly deleted.
EPHEMERAL = 1 << 6;
const EPHEMERAL = 1 << 6;
}
}

Expand Down

0 comments on commit 4fcd7ef

Please sign in to comment.