Skip to content

Commit

Permalink
feat(core): implement 'bot' class that implements the various classes
Browse files Browse the repository at this point in the history
  • Loading branch information
4x8Matrix committed Aug 25, 2024
1 parent 7a6ff28 commit 613ac37
Show file tree
Hide file tree
Showing 26 changed files with 611 additions and 214 deletions.
File renamed without changes.
37 changes: 6 additions & 31 deletions development.luau
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
local net = require("@lune/net")
local DiscordLuau = require("@core/init")

local manager = require("@websocket/manager")
local logger = require("@vendor/logger")

local gatewayTypes = require("@api-types/gateway/types")

local DISCORD_BOT_TOKEN = ""

local instance = manager.new({
token = DISCORD_BOT_TOKEN,
intents = 512,
webSocketVersion = 10,
})

local info = net.request({
url = "https://discord.com/api/gateway/bot",
headers = {
["authorization"] = `Bot {DISCORD_BOT_TOKEN}`,
["content-type"] = "application/json",
},
method = "GET",
local discordBot = DiscordLuau.Bot.new({
token = "",
intents = 0,
})

local loggerObject = logger.new("development")

loggerObject:info(`Requesting connect!`)

instance:connectAsync(net.jsonDecode(info.body)):await()

instance.onAllShardsReady:listenOnce(function(data)
local payload = data.payload :: gatewayTypes.ReadyPayload

loggerObject:info(`All shards are Ready! Logged in as: {payload.d.user.username}#{payload.d.user.discriminator}`)
discordBot:connectAsync():after(function()
print("Connected to Discord!")
end)
57 changes: 57 additions & 0 deletions packages/core/src/bot.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local future = require("@vendor/future")
local logger = require("@vendor/logger")

local gateway = require("@rest/gateway")

local state = require("@classes/state")

local DISCORD_VERSION = 10

local Bot = {}

Bot.Interface = {}
Bot.Prototype = {}

function Bot.Prototype.queryGatewayInformation(self: Bot)
return future.new(function()
local request = self.state.rest:newRequest()

local status, response = gateway.getGatewayBotAsync(request):await()

assert(status == "Fulfilled", response)

return response
end)
end

function Bot.Prototype.connectAsync(self: Bot)
return future.new(function()
local status, gatewayInformation = self:queryGatewayInformation():await()

assert(status == "Fulfilled", gatewayInformation)

self.state.webSocketManager:connectAsync(gatewayInformation):await()
end)
end

function Bot.Interface.new(options: {
token: string,
intents: number,
})
local self = setmetatable(
{
state = state.new(options.token, options.intents, DISCORD_VERSION),
logger = logger.new("Bot"),
} :: Bot,
{ __index = Bot.Prototype }
)

return self
end

export type Bot = typeof(Bot.Prototype) & {
state: state.State,
logger: logger.Logger,
}

return Bot.Interface
5 changes: 5 additions & 0 deletions packages/core/src/init.luau
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
local bot = require("@core/bot")

local DiscordLuau = {}

DiscordLuau.Bot = bot
export type Bot = bot.Bot

return DiscordLuau

-- tehe, secret egg? The thunder before the storm? Who knows - all I know is that Discord-Luau is going to be super-charged after this..
Expand Down
4 changes: 3 additions & 1 deletion packages/rest/src/application.luau
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function Application.getCurrentApplicationAsync(
instance:setMethod("GET")
instance:setUrl(string.format(restEndpoints.GetCurrentApplication, applicationId))

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand Down
8 changes: 6 additions & 2 deletions packages/rest/src/applicationRoleConnectionMetadata.luau
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function ApplicationRoleConnectionMetadata.getApplicationRoleConnectionMetadataR
instance:setMethod("GET")
instance:setUrl(string.format(restEndpoints.GetApplicationRoleConnectionMetadataRecords, applicationId))

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand All @@ -44,7 +46,9 @@ function ApplicationRoleConnectionMetadata.updateApplicationRoleConnectionMetada
instance:setMethod("PUT")
instance:setUrl(string.format(restEndpoints.UpdateApplicationRoleConnectionMetadataRecords, applicationId))

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand Down
4 changes: 3 additions & 1 deletion packages/rest/src/auditLog.luau
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ function AuditLog.getGuildAuditLogAsync(
instance:setMethod("GET")
instance:setUrl(string.format(restEndpoints.GetGuildAuditLog, guildId))

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand Down
16 changes: 12 additions & 4 deletions packages/rest/src/autoModeration.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function AutoModeration.listAutoModerationRulesForGuildAsync(
instance:setMethod("GET")
instance:setUrl(string.format(restEndpoints.ListAutoModerationRulesForGuild, guildId))

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand All @@ -44,7 +46,9 @@ function AutoModeration.getAutoModerationRulesForGuildAsync(
instance:setMethod("GET")
instance:setUrl(string.format(restEndpoints.GetAutoModerationRule, guildId, autoModerationRuleId))

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand All @@ -65,7 +69,9 @@ function AutoModeration.createAutoModerationRuleAsync(
instance:setBody(serde.encode("json", jsonParams, true))
instance:addHeader("x-audit-log-reason", auditLogReason :: string)

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand All @@ -87,7 +93,9 @@ function AutoModeration.modifyAutoModerationRuleAsync(
instance:setBody(serde.encode("json", jsonParams, true))
instance:addHeader("x-audit-log-reason", auditLogReason :: string)

local response = instance:executeAsync():await()
local status, response = instance:executeAsync():await()

assert(status == "Fulfilled", response)

return response.body
end)
Expand Down
Loading

0 comments on commit 613ac37

Please sign in to comment.