refactor step 1
This commit is contained in:
parent
17b5a5820d
commit
8e8dd205b2
@ -1,8 +1,8 @@
|
|||||||
package main
|
package commands
|
||||||
|
|
||||||
import "github.com/bwmarrin/discordgo"
|
import "github.com/bwmarrin/discordgo"
|
||||||
|
|
||||||
var commands = []*discordgo.ApplicationCommand{
|
var Commands = []*discordgo.ApplicationCommand{
|
||||||
{
|
{
|
||||||
Name: "test1",
|
Name: "test1",
|
||||||
Description: "Showcase of a basic slash command",
|
Description: "Showcase of a basic slash command",
|
||||||
74
handlers.go
74
handlers.go
@ -1,74 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
||||||
|
|
||||||
if m.Author.ID == s.State.User.ID {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch m.Content {
|
|
||||||
case "test":
|
|
||||||
var serverListString string
|
|
||||||
for i, server := range ServerList {
|
|
||||||
serverListString = serverListString + fmt.Sprintf("\n %d) ", i+1) + server.Name
|
|
||||||
}
|
|
||||||
s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
|
|
||||||
Title: "Servers",
|
|
||||||
Fields: []*discordgo.MessageEmbedField{
|
|
||||||
{
|
|
||||||
Value: serverListString,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Type: discordgo.EmbedTypeArticle,
|
|
||||||
})
|
|
||||||
|
|
||||||
case "!test":
|
|
||||||
s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
|
|
||||||
Title: "Servers",
|
|
||||||
Fields: []*discordgo.MessageEmbedField{
|
|
||||||
{
|
|
||||||
Value: "test",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Type: discordgo.EmbedTypeArticle,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func CommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|
||||||
data := i.ApplicationCommandData()
|
|
||||||
switch data.Name {
|
|
||||||
case "init":
|
|
||||||
AddServer(i.GuildID, s)
|
|
||||||
case "test1":
|
|
||||||
err := s.InteractionRespond(
|
|
||||||
i.Interaction,
|
|
||||||
&discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
// Handle the error
|
|
||||||
}
|
|
||||||
case "read_back":
|
|
||||||
err := s.InteractionRespond(
|
|
||||||
i.Interaction,
|
|
||||||
&discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: data.Options[0].Value.(string),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
// Handle the error
|
|
||||||
}
|
|
||||||
case "delete":
|
|
||||||
deleteMessages(data, s, i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,11 +1,12 @@
|
|||||||
package main
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"kleincordBot/services"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func deleteMessages(data discordgo.ApplicationCommandInteractionData, s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func DeleteCommand(data discordgo.ApplicationCommandInteractionData, s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
amount, _ := strconv.Atoi(data.Options[0].Value.(string))
|
amount, _ := strconv.Atoi(data.Options[0].Value.(string))
|
||||||
if amount > 100 {
|
if amount > 100 {
|
||||||
amount = 100
|
amount = 100
|
||||||
@ -42,7 +43,10 @@ func deleteMessages(data discordgo.ApplicationCommandInteractionData, s *discord
|
|||||||
println(message.Content + " deleted")
|
println(message.Content + " deleted")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
_, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
|
||||||
Content: strconv.Itoa(len(messages)) + " Message deleted",
|
Content: strconv.Itoa(len(messages)) + " Message deleted",
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
services.HandleError(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
1
handlers/handlers.go
Normal file
1
handlers/handlers.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package handlers
|
||||||
42
handlers/messageHandler.go
Normal file
42
handlers/messageHandler.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"kleincordBot/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MessageTest(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
var serverListString string
|
||||||
|
for i, server := range services.ServerList {
|
||||||
|
serverListString = serverListString + fmt.Sprintf("\n %d) ", i+1) + server.Name
|
||||||
|
}
|
||||||
|
_, err := s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
|
||||||
|
Title: "Servers",
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{
|
||||||
|
Value: serverListString,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Type: discordgo.EmbedTypeArticle,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
services.HandleError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MessageTest2(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
|
_, err := s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
|
||||||
|
Title: "Servers",
|
||||||
|
Fields: []*discordgo.MessageEmbedField{
|
||||||
|
{
|
||||||
|
Value: "test",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Type: discordgo.EmbedTypeArticle,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
services.HandleError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
11
main.go
11
main.go
@ -4,6 +4,9 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"kleincordBot/commands"
|
||||||
|
"kleincordBot/routers"
|
||||||
|
"kleincordBot/services"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -22,22 +25,22 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
initServers()
|
services.InitServers()
|
||||||
|
|
||||||
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
discord.AddHandler(MessageHandler)
|
discord.AddHandler(routers.MessageRouter)
|
||||||
discord.AddHandler(CommandHandler)
|
discord.AddHandler(routers.CommandRouter)
|
||||||
|
|
||||||
err = discord.Open()
|
err = discord.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Cannot open the session: %v", err)
|
log.Fatalf("Cannot open the session: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = discord.ApplicationCommandBulkOverwrite(discord.State.User.ID, *GuildID, commands)
|
_, err = discord.ApplicationCommandBulkOverwrite(discord.State.User.ID, *GuildID, commands.Commands)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err.Error())
|
print(err.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
41
routers/commandRouter.go
Normal file
41
routers/commandRouter.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package routers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"kleincordBot/handlers"
|
||||||
|
"kleincordBot/services"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CommandRouter(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
data := i.ApplicationCommandData()
|
||||||
|
switch data.Name {
|
||||||
|
case "init":
|
||||||
|
services.AddServer(i.GuildID, s)
|
||||||
|
case "test1":
|
||||||
|
err := s.InteractionRespond(
|
||||||
|
i.Interaction,
|
||||||
|
&discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
services.HandleError(err)
|
||||||
|
}
|
||||||
|
case "read_back":
|
||||||
|
err := s.InteractionRespond(
|
||||||
|
i.Interaction,
|
||||||
|
&discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: data.Options[0].Value.(string),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
services.HandleError(err)
|
||||||
|
}
|
||||||
|
case "delete":
|
||||||
|
handlers.DeleteCommand(data, s, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
19
routers/messageRouter.go
Normal file
19
routers/messageRouter.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package routers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
"kleincordBot/handlers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MessageRouter(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch m.Content {
|
||||||
|
case "test":
|
||||||
|
handlers.MessageTest(s, m)
|
||||||
|
case "!test":
|
||||||
|
handlers.MessageTest2(s, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
5
services/Error.go
Normal file
5
services/Error.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
func HandleError(err error) {
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package main
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -14,7 +14,7 @@ type Server struct {
|
|||||||
|
|
||||||
var ServerList []Server
|
var ServerList []Server
|
||||||
|
|
||||||
func initServers() {
|
func InitServers() {
|
||||||
ServerList = readJson()
|
ServerList = readJson()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package main
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
Loading…
x
Reference in New Issue
Block a user