2020-01-02 19:53:08 +00:00
|
|
|
package discord
|
|
|
|
|
2020-10-29 20:25:03 +00:00
|
|
|
import (
|
|
|
|
"strconv"
|
2021-01-10 19:07:04 +00:00
|
|
|
"strings"
|
2021-05-20 22:47:44 +00:00
|
|
|
"time"
|
2020-10-29 20:25:03 +00:00
|
|
|
)
|
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// https://discord.com/developers/docs/resources/channel#channel-object
|
2020-01-02 19:53:08 +00:00
|
|
|
type Channel struct {
|
2020-05-23 16:01:08 +00:00
|
|
|
// ID is the id of this channel.
|
2020-11-22 14:48:36 +00:00
|
|
|
ID ChannelID `json:"id"`
|
|
|
|
// GuildID is the id of the guild.
|
|
|
|
GuildID GuildID `json:"guild_id,omitempty"`
|
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// Type is the type of channel.
|
2020-11-21 17:40:54 +00:00
|
|
|
Type ChannelType `json:"type,omitempty"`
|
2020-11-22 14:48:36 +00:00
|
|
|
// NSFW specifies whether the channel is nsfw.
|
|
|
|
NSFW bool `json:"nsfw,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// Position is the sorting position of the channel.
|
|
|
|
Position int `json:"position,omitempty"`
|
|
|
|
// Permissions are the explicit permission overrides for members and roles.
|
|
|
|
Permissions []Overwrite `json:"permission_overwrites,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// Name is the name of the channel (2-100 characters).
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
// Topic is the channel topic (0-1024 characters).
|
|
|
|
Topic string `json:"topic,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// LastMessageID is the id of the last message sent in this channel (may
|
|
|
|
// not point to an existing or valid message).
|
2020-11-22 14:48:36 +00:00
|
|
|
LastMessageID MessageID `json:"last_message_id,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// VoiceBitrate is the bitrate (in bits) of the voice channel.
|
|
|
|
VoiceBitrate uint `json:"bitrate,omitempty"`
|
|
|
|
// VoiceUserLimit is the user limit of the voice channel.
|
|
|
|
VoiceUserLimit uint `json:"user_limit,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// UserRateLimit is the amount of seconds a user has to wait before sending
|
|
|
|
// another message (0-21600). Bots, as well as users with the permission
|
|
|
|
// manage_messages or manage_channel, are unaffected.
|
|
|
|
UserRateLimit Seconds `json:"rate_limit_per_user,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// DMRecipients are the recipients of the DM.
|
|
|
|
DMRecipients []User `json:"recipients,omitempty"`
|
|
|
|
// Icon is the icon hash.
|
|
|
|
Icon Hash `json:"icon,omitempty"`
|
|
|
|
// DMOwnerID is the id of the DM creator.
|
2020-11-22 14:48:36 +00:00
|
|
|
DMOwnerID UserID `json:"owner_id,omitempty"`
|
2020-05-23 16:01:08 +00:00
|
|
|
|
|
|
|
// AppID is the application id of the group DM creator if it is
|
|
|
|
// bot-created.
|
2020-11-22 14:48:36 +00:00
|
|
|
AppID AppID `json:"application_id,omitempty"`
|
2020-01-02 19:53:08 +00:00
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// CategoryID is the id of the parent category for a channel (each parent
|
|
|
|
// category can contain up to 50 channels).
|
2020-11-22 14:48:36 +00:00
|
|
|
CategoryID ChannelID `json:"parent_id,omitempty"`
|
2020-05-23 16:01:08 +00:00
|
|
|
// LastPinTime is when the last pinned message was pinned.
|
2020-01-02 19:53:08 +00:00
|
|
|
LastPinTime Timestamp `json:"last_pin_timestamp,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-05-20 22:47:44 +00:00
|
|
|
// CreatedAt returns a time object representing when the channel was created.
|
|
|
|
func (ch Channel) CreatedAt() time.Time {
|
|
|
|
return ch.ID.Time()
|
|
|
|
}
|
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// Mention returns a mention of the channel.
|
2020-01-24 05:45:47 +00:00
|
|
|
func (ch Channel) Mention() string {
|
2020-09-24 18:43:20 +00:00
|
|
|
return ch.ID.Mention()
|
2020-01-24 05:45:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// IconURL returns the URL to the channel icon in the PNG format.
|
|
|
|
// An empty string is returned if there's no icon.
|
2020-03-05 23:49:44 +00:00
|
|
|
func (ch Channel) IconURL() string {
|
2020-05-23 16:01:08 +00:00
|
|
|
return ch.IconURLWithType(PNGImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IconURLWithType returns the URL to the channel icon using the passed
|
|
|
|
// ImageType. An empty string is returned if there's no icon.
|
|
|
|
//
|
|
|
|
// Supported ImageTypes: PNG, JPEG, WebP
|
|
|
|
func (ch Channel) IconURLWithType(t ImageType) string {
|
2020-03-05 23:49:44 +00:00
|
|
|
if ch.Icon == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return "https://cdn.discordapp.com/channel-icons/" +
|
2020-05-23 16:01:08 +00:00
|
|
|
ch.ID.String() + "/" + t.format(ch.Icon)
|
2020-03-05 23:49:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 19:53:08 +00:00
|
|
|
type ChannelType uint8
|
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// https://discord.com/developers/docs/resources/channel#channel-object-channel-types
|
2020-05-11 02:30:34 +00:00
|
|
|
var (
|
2020-05-23 16:01:08 +00:00
|
|
|
// GuildText is a text channel within a server.
|
|
|
|
GuildText ChannelType = 0
|
|
|
|
// DirectMessage is a direct message between users.
|
2020-05-11 02:30:34 +00:00
|
|
|
DirectMessage ChannelType = 1
|
2020-05-23 16:01:08 +00:00
|
|
|
// GuildVoice is a voice channel within a server.
|
|
|
|
GuildVoice ChannelType = 2
|
|
|
|
// GroupDM is a direct message between multiple users.
|
|
|
|
GroupDM ChannelType = 3
|
|
|
|
// GuildCategory is an organizational category that contains up to 50
|
|
|
|
// channels.
|
2020-05-11 02:30:34 +00:00
|
|
|
GuildCategory ChannelType = 4
|
2020-05-23 16:01:08 +00:00
|
|
|
// GuildNews is a channel that users can follow and crosspost into their
|
|
|
|
// own server.
|
|
|
|
GuildNews ChannelType = 5
|
|
|
|
// GuildStore is a channel in which game developers can sell their game on
|
|
|
|
// Discord.
|
|
|
|
GuildStore ChannelType = 6
|
2020-01-02 19:53:08 +00:00
|
|
|
)
|
|
|
|
|
2020-05-23 16:01:08 +00:00
|
|
|
// https://discord.com/developers/docs/resources/channel#overwrite-object
|
2020-01-02 19:53:08 +00:00
|
|
|
type Overwrite struct {
|
2020-05-23 16:01:08 +00:00
|
|
|
// ID is the role or user id.
|
2020-08-18 00:10:43 +00:00
|
|
|
ID Snowflake `json:"id"`
|
2020-10-28 21:49:30 +00:00
|
|
|
// Type indicates the entity overwritten: role or member.
|
2020-10-28 22:34:14 +00:00
|
|
|
Type OverwriteType `json:"type"`
|
2020-05-23 16:01:08 +00:00
|
|
|
// Allow is a permission bit set for granted permissions.
|
2020-08-18 00:10:43 +00:00
|
|
|
Allow Permissions `json:"allow,string"`
|
2020-05-23 16:01:08 +00:00
|
|
|
// Deny is a permission bit set for denied permissions.
|
2020-08-18 00:10:43 +00:00
|
|
|
Deny Permissions `json:"deny,string"`
|
|
|
|
}
|
|
|
|
|
2020-10-28 21:49:30 +00:00
|
|
|
// OverwriteType is an enumerated type to indicate the entity being overwritten:
|
|
|
|
// role or member
|
2020-10-28 21:08:29 +00:00
|
|
|
type OverwriteType uint8
|
2020-01-02 19:53:08 +00:00
|
|
|
|
|
|
|
const (
|
2020-05-23 16:01:08 +00:00
|
|
|
// OverwriteRole is an overwrite for a role.
|
2020-10-28 21:08:29 +00:00
|
|
|
OverwriteRole OverwriteType = iota
|
2020-05-23 16:01:08 +00:00
|
|
|
// OverwriteMember is an overwrite for a member.
|
2020-10-28 21:08:29 +00:00
|
|
|
OverwriteMember
|
2020-01-02 19:53:08 +00:00
|
|
|
)
|
2020-10-29 20:25:03 +00:00
|
|
|
|
2020-10-30 20:41:04 +00:00
|
|
|
// UnmarshalJSON unmarshalls both a string-quoted number and a regular number
|
2020-10-29 20:25:03 +00:00
|
|
|
// into OverwriteType. We need to do this because Discord is so bad that they
|
|
|
|
// can't even handle 1s and 0s properly.
|
|
|
|
func (otype *OverwriteType) UnmarshalJSON(b []byte) error {
|
2021-01-10 19:07:04 +00:00
|
|
|
s := strings.Trim(string(b), `"`)
|
|
|
|
|
|
|
|
// It has been observed that discord still uses the "legacy" string
|
|
|
|
// overwrite types in at least the guild create event.
|
|
|
|
// Therefore this string check.
|
|
|
|
switch s {
|
|
|
|
case "role":
|
|
|
|
*otype = OverwriteRole
|
|
|
|
return nil
|
|
|
|
case "member":
|
|
|
|
*otype = OverwriteMember
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-29 20:25:03 +00:00
|
|
|
|
2021-01-10 19:07:04 +00:00
|
|
|
u, err := strconv.ParseUint(s, 10, 8)
|
2020-10-29 20:25:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*otype = OverwriteType(u)
|
|
|
|
return nil
|
|
|
|
}
|