Project start

Co-Authored-By: Lee Hyun <ink0416@naver.com>
Co-Authored-By: yky4589 <8479056+yky4589@users.noreply.github.com>
Co-Authored-By: Choi Minseo <minseo0388@outlook.com>
Co-Authored-By: Aki <akiacode@users.noreply.github.com>
This commit is contained in:
Helloyunho 2020-10-22 01:30:42 +09:00
parent c21fab2aff
commit 4b9123f5f6
19 changed files with 472 additions and 1 deletions

View file

@ -0,0 +1,7 @@
export const DISCORD_API_URL = 'https://discord.com/api' //여기 v8 들어가야하는거 아닌가... <- gateway.ts 참조바람 왜 거기 endpoint에서 import에서 에러나지 왜??
export const DISCORD_GATEWAY_URL = 'wss://gateway.discord.com'
export const DISCORD_CDN_URL = 'https://cdn.discordapp.com'
export const DISCORD_API_VERSION = 8

114
src/models/gateway.ts Normal file
View file

@ -0,0 +1,114 @@
import { inflate } from 'https://deno.land/x/denoflate/mod.ts'
import {
DISCORD_GATEWAY_URL,
DISCORD_API_VERSION
} from '../consts/urlsAndVersions.ts'
import { GatewayResponse } from '../types/gatewayResponse.ts'
import { GatewayOpcodes, GatewayIntents } from '../types/gatewayTypes.ts'
/**
* Handles Discord gateway connection.
* You should not use this and rather use Client class.
*
* @beta
*/
class Gateway {
websocket: WebSocket
token: string
intents: [GatewayIntents]
connected = false
initialized = false
heartbeatInterval = 0
heartbeatIntervalID?: number
sequenceID?: number
heartbeatServerResponded = false
constructor (token: string, intents: [GatewayIntents]) {
this.websocket = new WebSocket(
`${DISCORD_GATEWAY_URL}/?v=${DISCORD_API_VERSION}&encoding=json`
)
this.token = token
this.intents = intents
}
onopen () {
this.connected = true
}
onmessage (event: MessageEvent) {
let data = event.data
if (data instanceof ArrayBuffer) {
data = new Uint8Array(data)
}
if (data instanceof Uint8Array) {
const dataSuffix = data.slice(-4)
if (
dataSuffix[0] === 0 &&
dataSuffix[1] === 0 &&
dataSuffix[2] === 0xff &&
dataSuffix[3] === 0xff
) {
data = inflate(data)
}
}
const { op, d, s, t }: GatewayResponse = JSON.parse(data)
switch (op) {
case GatewayOpcodes.HELLO:
this.heartbeatInterval = d.heartbeat_interval
this.heartbeatIntervalID = setInterval(() => {
this.websocket.send(
JSON.stringify({
op: GatewayOpcodes.HEARTBEAT,
d: this.sequenceID ?? null
})
)
if (this.heartbeatServerResponded) {
this.heartbeatServerResponded = false
} else {
// TODO: Add heartbeat failed error
}
}, this.heartbeatInterval)
this.websocket.send(
JSON.stringify({
token: this.token,
properties: {
$os: Deno.build.os,
$browser: 'discord.deno',
$device: 'discord.deno'
},
compress: true,
shard: [0, 1], // TODO: Make sharding possible
intents: this.intents.reduce(
(previous, current) => previous | current,
0
),
presence: {
// TODO: User should can customize this
status: 'online',
since: null,
afk: false
}
})
)
break
case GatewayOpcodes.HEARTBEAT_ACK:
this.heartbeatServerResponded = true
break
case GatewayOpcodes.DISPATCH:
switch (t) {
}
default:
return
}
}
}
export { Gateway }

10
src/structures/base.ts Normal file
View file

@ -0,0 +1,10 @@
// 일단 대충 여러 봇 라이브러리에서 본 구조 가져오는 중..
class Base {
id?: string
constructor (id: string | undefined) {
if (id) {
this.id = id
}
}
}

View file

0
src/structures/embed.ts Normal file
View file

0
src/structures/guild.ts Normal file
View file

View file

@ -0,0 +1 @@
// 여긴가

View file

66
src/types/endpoint.ts Normal file
View file

@ -0,0 +1,66 @@
//Being Written by Choi Donghan, Catry
import {
DISCORD_API_URL,
DISCORD_API_VERSION,
DISCORD_CDN_URL
} from '../consts/urlsAndVersions.ts'
//Guild Endpoints
const GUILDS = `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds`
const GUILD = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}`
const GUILD_ICON = (guildID: string, iconID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/${iconID}`
const GUILD_BANNER = (guildID: string, iconID: string) =>
`${DISCORD_CDN_URL}/${guildID}/${iconID}`
const GUILD_AUDIT_LOGS = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/audit-logs`
const GUILD_WIDGET = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/widget`
const GUILD_EMOJI = (guildID: string, emoji_id: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/emojis/${emoji_id}`
const GUILD_EMOJIS = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/emojis`
const GUILD_REGIONS = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/regions`
const GUILD_ROLE = (guildID: string, roleID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/roles/${roleID}`
const GUILD_ROLES = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/roles`
const GUILD_MEMBER_ROLE = (guildID: string, memberID: string, roleID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/members/${memberID}/roles/${roleID}`
const GUILD_INTEGRATION = (guildID: string, integrationID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/integrations/${integrationID}`
const GUILD_INTEGRATIONS = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/integrations`
const GUILD_INTEGARTION_SYNC = (guildID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/integrations?include_appilications=true`
//Channel Endpoints
const CHANNEL = (channelID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}`
const CHANNELS = (channelID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${channelID}/channels`
const CHANNEL_MESSAGES = (channelID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages`
const CHANNEL_MESSAGE = (channelID: string, messageID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}`
const CHANNEL_CROSSPOST = (channelID: string, messageID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}/crosspost`
const MESSAGE_REACTION = (
channelID: string,
messageID: string,
emoji: string
) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/channels/${channelID}/messages/${messageID}/reactions/${emoji}/@me`
//User Endpoints
const USER = (userID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/${userID}`
const USER_AVATAR_DEFAULT = (iconID: number) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/embed/avatars${iconID}.png`
const USER_AVATAR = (userID: string, iconID: string) =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/avatars/${userID}/${iconID}.png`
const USER_CREATE_DM = () =>
`${DISCORD_API_URL}/v${DISCORD_API_VERSION}/users/@me/channels`

View file

@ -0,0 +1,14 @@
import { GatewayOpcodes } from '../types/gatewayTypes.ts'
/**
* Gateway response from Discord.
*
*/
interface GatewayResponse {
op: GatewayOpcodes
d: any
s?: number
t?: string
}
export { GatewayResponse }

58
src/types/gatewayTypes.ts Normal file
View file

@ -0,0 +1,58 @@
// https://discord.com/developers/docs/topics/opcodes-and-status-codes#gateway
/**
* Gateway OPcodes from Discord docs.
*/
enum GatewayOpcodes { // 문서를 확인해본 결과 Opcode 5번은 비어있다. - UnderC -
DISPATCH = 0,
HEARTBEAT = 1,
IDENTIFY = 2,
PRESENCE_UPDATE = 3,
VOICE_STATE_UPDATE = 4,
RESUME = 6,
RECONNECT = 7,
REQUEST_GUILD_MEMBERS = 8,
INVALID_SESSION = 9,
HELLO = 10,
HEARTBEAT_ACK = 11
}
/**
* Gateway Close Codes from Discord docs.
*/
enum GatewayCloseCodes {
UNKNOWN_ERROR = 4000,
UNKNOWN_OPCODE = 4001,
DECODE_ERROR = 4002,
NOT_AUTHENTICATED = 4003,
AUTHENTICATION_FAILED = 4004,
ALREADY_AUTHENTICATED = 4005,
INVALID_SEQ = 4007,
RATE_LIMITED = 4008,
SESSION_TIMED_OUT = 4009,
INVALID_SHARD = 4010,
SHARDING_REQUIRED = 4011,
INVALID_API_VERSION = 4012,
INVALID_INTENTS = 4013,
DISALLOWED_INTENTS = 4014
}
enum GatewayIntents {
GUILDS = 1 << 0,
GUILD_MEMBERS = 1 << 1,
GUILD_BANS = 1 << 2,
GUILD_EMOJIS = 1 << 3,
GUILD_INTEGRATIONS = 1 << 4,
GUILD_WEBHOOKS = 1 << 5,
GUILD_INVITES = 1 << 6,
GUILD_VOICE_STATES = 1 << 7,
GUILD_PRESENCES = 1 << 8,
GUILD_MESSAGES = 1 << 9,
GUILD_MESSAGE_REACTIONS = 1 << 10,
GUILD_MESSAGE_TYPING = 1 << 11,
DIRECT_MESSAGES = 1 << 12,
DIRECT_MESSAGE_REACTIONS = 1 << 13,
DIRECT_MESSAGE_TYPING = 1 << 13
}
export { GatewayCloseCodes, GatewayOpcodes, GatewayIntents }

View file

@ -0,0 +1,45 @@
// https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
// 귀하는 해갈
// 그렇습니다
//이거 필요한건가 -와갈드- -UnderC-
enum PermissionFlags {
CREATE_INSTANT_INVITE = 0x00000001,
KICK_MEMBERS = 0x00000002,
BAN_MEMBERS = 0x00000004,
ADMINISTRATOR = 0x00000008,
MANAGE_CHANNELS = 0x00000010,
MANAGE_GUILD = 0x00000020,
ADD_REACTIONS = 0x00000040,
VIEW_AUDIT_LOG = 0x00000080,
PRIORITY_SPEAKER = 0x00000100,
STREAM = 0x00000200,
VIEW_CHANNEL = 0x00000400,
SEND_MESSAGES = 0x00000800,
SEND_TTS_MESSAGES = 0x00001000,
MANAGE_MESSAGES = 0x00002000,
EMBED_LINKS = 0x00004000,
ATTACH_FILES = 0x00008000,
READ_MESSAGE_HISTORY = 0x00010000,
MENTION_EVERYONE = 0x00020000,
USE_EXTERNAL_EMOJIS = 0x00040000,
VIEW_GUILD_INSIGHTS = 0x00080000,
CONNECT = 0x00100000,
SPEAK = 0x00200000,
MUTE_MEMBERS = 0x00400000,
DEAFEN_MEMBERS = 0x00800000,
MOVE_MEMBERS = 0x01000000,
USE_VAD = 0x02000000,
CHANGE_NICKNAME = 0x04000000,
MANAGE_NICKNAMES = 0x08000000,
MANAGE_ROLES = 0x10000000,
MANAGE_WEBHOOKS = 0x20000000,
MANAGE_EMOJIS = 0x40000000
}

29
src/types/voiceTypes.ts Normal file
View file

@ -0,0 +1,29 @@
// https://discord.com/developers/docs/topics/opcodes-and-status-codes#voice
enum VoiceOpcodes { // VoiceOpcodes 추가 - UnderC -
IDENTIFY = 0,
SELECT_PROTOCOL = 1,
READY = 2,
HEARTBEAT = 3,
SESSION_DESCRIPTION = 4,
SPEAKING = 6,
HEARTBEAT_ACK = 6,
RESUME = 7,
HELLO = 8,
RESUMED = 9,
CLIENT_DISCONNECT = 13
}
enum VoiceCloseCodes {
UNKNOWN_OPCODE = 4001,
NOT_AUTHENTICATED = 4003,
AUTHENTICATION_FAILED = 4004,
ALREADY_AUTHENTICATED = 4005,
SESSION_NO_LONGER_VALID = 4006,
SESSION_TIMEOUT = 4009,
SERVER_NOT_FOUNT = 4011,
UNKNOWN_PROTOCOL = 4012,
DISCONNECTED = 4014,
VOICE_SERVER_CRASHED = 4015,
UNKNOWN_ENCRYPTION_MODE = 4016
}