welcome message
This commit is contained in:
@@ -13,7 +13,7 @@ services:
|
|||||||
DISCORD_CHANNEL_ID: ${DISCORD_CHANNEL_ID}
|
DISCORD_CHANNEL_ID: ${DISCORD_CHANNEL_ID}
|
||||||
ARCHIVE_DURATION: ${ARCHIVE_DURATION}
|
ARCHIVE_DURATION: ${ARCHIVE_DURATION}
|
||||||
THREAD_NAME: ${THREAD_NAME}
|
THREAD_NAME: ${THREAD_NAME}
|
||||||
|
THREAD_MESSAGE: ${THREAD_MESSAGE}
|
||||||
cron:
|
cron:
|
||||||
image: alpine
|
image: alpine
|
||||||
container_name: discord_status_cron
|
container_name: discord_status_cron
|
||||||
|
|||||||
@@ -17,6 +17,14 @@ type ThreadRequest struct {
|
|||||||
Auto_archive_duration int `json:"auto_archive_duration"`
|
Auto_archive_duration int `json:"auto_archive_duration"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MessageRequest struct {
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ThreadResponse struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
type StatusBotSender struct {
|
type StatusBotSender struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,14 +43,33 @@ func (bot *StatusBotSender) GetThreadName() string {
|
|||||||
return base + " (" + date + ")"
|
return base + " (" + date + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *StatusBotSender) BuildBody() ([]byte, error) {
|
func (bot *StatusBotSender) BuildThreadBody() ([]byte, error) {
|
||||||
return json.Marshal(ThreadRequest{
|
return json.Marshal(ThreadRequest{
|
||||||
Name: bot.GetThreadName(),
|
Name: bot.GetThreadName(),
|
||||||
Auto_archive_duration: bot.GetArchiveDuration(),
|
Auto_archive_duration: bot.GetArchiveDuration(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *StatusBotSender) BuildRequest(body []byte) (*http.Request, error) {
|
func (bot *StatusBotSender) BuildThreaMessagedBody() ([]byte, error) {
|
||||||
|
{
|
||||||
|
return json.Marshal(MessageRequest{
|
||||||
|
Content: os.Getenv("THREAD_MESSAGE"),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *StatusBotSender) BuildThreadMessageRequest(body []byte, channel_id string) (*http.Request, error) {
|
||||||
|
req, err := http.NewRequest(
|
||||||
|
"POST",
|
||||||
|
DISCORD_API+"/channels/"+channel_id+"/messages",
|
||||||
|
bytes.NewBuffer(body),
|
||||||
|
)
|
||||||
|
req.Header.Set("Authorization", "Bot "+os.Getenv("DISCORD_BOT_TOKEN"))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
return req, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *StatusBotSender) BuildThreadRequest(body []byte) (*http.Request, error) {
|
||||||
req, err := http.NewRequest(
|
req, err := http.NewRequest(
|
||||||
"POST",
|
"POST",
|
||||||
DISCORD_API+"/channels/"+os.Getenv("DISCORD_CHANNEL_ID")+"/threads",
|
DISCORD_API+"/channels/"+os.Getenv("DISCORD_CHANNEL_ID")+"/threads",
|
||||||
@@ -54,11 +81,24 @@ func (bot *StatusBotSender) BuildRequest(body []byte) (*http.Request, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bot *StatusBotSender) SendCreateThreadRequest() error {
|
func (bot *StatusBotSender) SendCreateThreadRequest() error {
|
||||||
body, _ := bot.BuildBody()
|
body, _ := bot.BuildThreadBody()
|
||||||
req, _ := bot.BuildRequest(body)
|
req, _ := bot.BuildThreadRequest(body)
|
||||||
log.Println(req.Method, req.URL.String(), req.Header.Values("Authorization"))
|
|
||||||
log.Println("Sending request to create thread:", string(body))
|
log.Println("Sending request to create thread:", string(body))
|
||||||
res, _ := http.DefaultClient.Do(req)
|
res, _ := http.DefaultClient.Do(req)
|
||||||
log.Println("Response status:", res.Status)
|
log.Println("Response status:", res.Status)
|
||||||
|
var tr ThreadResponse
|
||||||
|
json.NewDecoder(res.Body).Decode(&tr)
|
||||||
|
|
||||||
|
if tr.ID != "" {
|
||||||
|
msg := os.Getenv("THREAD_MESSAGE")
|
||||||
|
msg_body, _ := bot.BuildThreaMessagedBody()
|
||||||
|
msg_req, _ := bot.BuildThreadMessageRequest(msg_body, tr.ID)
|
||||||
|
log.Println("Sending message to thread:", msg)
|
||||||
|
_, err := http.DefaultClient.Do(msg_req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user