init command added
Delete command added
This commit is contained in:
parent
8421cc8f26
commit
26338f6b16
38
commands.go
Normal file
38
commands.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
var commands = []*discordgo.ApplicationCommand{
|
||||
{
|
||||
Name: "test1",
|
||||
Description: "Showcase of a basic slash command",
|
||||
},
|
||||
{
|
||||
Name: "init",
|
||||
Description: "init things",
|
||||
},
|
||||
{
|
||||
Name: "read_back",
|
||||
Description: "Showcase of a basic slash command2",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "text",
|
||||
Description: "text to read back",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
Description: "delete x amount recent messages in this channel",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "amount",
|
||||
Description: "max 100",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
80
handlers.go
Normal file
80
handlers.go
Normal file
@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
if m.Content == "test" {
|
||||
s.ChannelMessageSend(m.ChannelID, "werkt")
|
||||
}
|
||||
}
|
||||
|
||||
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{
|
||||
TTS: true,
|
||||
Content: "Hello world!",
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
// Handle the error
|
||||
}
|
||||
case "read_back":
|
||||
err := s.InteractionRespond(
|
||||
i.Interaction,
|
||||
&discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
TTS: true,
|
||||
Content: data.Options[0].Value.(string),
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
// Handle the error
|
||||
}
|
||||
case "delete":
|
||||
amount, _ := strconv.Atoi(data.Options[0].Value.(string))
|
||||
if amount > 100 {
|
||||
amount = 100
|
||||
}
|
||||
messages, err := s.ChannelMessages(i.ChannelID, amount, "", "", "")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = s.InteractionRespond(
|
||||
i.Interaction,
|
||||
&discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
TTS: true,
|
||||
Content: "deleting " + strconv.Itoa(len(messages)) + " messages",
|
||||
},
|
||||
},
|
||||
)
|
||||
message, err := s.ChannelMessageSend(i.ChannelID, "Please hold....")
|
||||
if err != nil {
|
||||
// Handle the error
|
||||
}
|
||||
for _, message := range messages {
|
||||
s.ChannelMessageDelete(i.ChannelID, message.ID)
|
||||
println(message.Content + " deleted")
|
||||
}
|
||||
s.ChannelMessageEdit(i.ChannelID, message.ID, strconv.Itoa(len(messages))+" Message deleted")
|
||||
}
|
||||
}
|
||||
43
initServer.go
Normal file
43
initServer.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Name string `json:"name"`
|
||||
GuildID string `json:"GuildID"`
|
||||
Webhooks []*discordgo.Webhook `json:"Webhooks"`
|
||||
}
|
||||
|
||||
func AddServer(GuildID string, s *discordgo.Session) Server {
|
||||
servers := readJson()
|
||||
preview, err := s.GuildPreview(GuildID)
|
||||
if err != nil {
|
||||
}
|
||||
Webhooks, _ := s.GuildWebhooks(GuildID)
|
||||
|
||||
server := Server{
|
||||
Name: preview.Name,
|
||||
GuildID: GuildID,
|
||||
Webhooks: Webhooks,
|
||||
}
|
||||
var exists bool
|
||||
|
||||
for i, oldServer := range servers {
|
||||
if oldServer.GuildID == server.GuildID {
|
||||
servers[i] = server
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
servers = append(servers, server)
|
||||
}
|
||||
|
||||
rankingsJson, _ := json.Marshal(servers)
|
||||
err = os.WriteFile("servers.json", rankingsJson, 0644)
|
||||
return server
|
||||
}
|
||||
29
main.go
29
main.go
@ -1,13 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/joho/godotenv"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var (
|
||||
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
|
||||
discord *discordgo.Session
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatalf(".env not found: %v", err)
|
||||
@ -19,4 +27,23 @@ func init() {
|
||||
log.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
discord.AddHandler(MessageHandler)
|
||||
discord.AddHandler(CommandHandler)
|
||||
|
||||
err = discord.Open()
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot open the session: %v", err)
|
||||
}
|
||||
|
||||
_, err = discord.ApplicationCommandBulkOverwrite(discord.State.User.ID, *GuildID, commands)
|
||||
if err != nil {
|
||||
print(err.Error())
|
||||
}
|
||||
|
||||
defer discord.Close()
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt)
|
||||
log.Println("Press Ctrl+C to exit")
|
||||
<-stop
|
||||
}
|
||||
|
||||
28
readJson.go
Normal file
28
readJson.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func readJson() []Server {
|
||||
// Open our jsonFile
|
||||
jsonFile, err := os.Open("servers.json")
|
||||
// if we os.Open returns an error then handle it
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println("Successfully Opened users.json")
|
||||
// defer the closing of our jsonFile so that we can parse it later on
|
||||
defer jsonFile.Close()
|
||||
|
||||
byteValue, _ := io.ReadAll(jsonFile)
|
||||
|
||||
var servers []Server
|
||||
|
||||
json.Unmarshal(byteValue, &servers)
|
||||
|
||||
return servers
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user