Created
March 8, 2024 13:45
-
-
Save abiriadev/d6eb08cddbe426fb0618d2eb146b1e64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"context" | |
"fmt" | |
"log" | |
"os" | |
"os/signal" | |
"strings" | |
"syscall" | |
"github.com/abiriadev/discord-garden/lib" | |
"github.com/bwmarrin/discordgo" | |
influxdb2 "github.com/influxdata/influxdb-client-go/v2" | |
) | |
var qapi, wapi = lib.InitClient("http://localhost:8086", os.Getenv("INFLUX"), "cl", "hello") | |
var gid = os.Getenv("GID") | |
func main() { | |
tok := os.Getenv("TOK") | |
discord, err := discordgo.New("Bot " + tok) | |
if err != nil { | |
panic(err) | |
} | |
discord.AddHandler(msg) | |
discord.Identify.Intents = discordgo.IntentsGuildMessages | |
commands := []*discordgo.ApplicationCommand{ | |
{ | |
Name: "ranking", | |
Description: "Server ranking", | |
}, | |
{ | |
Name: "garden", | |
Description: "Show my grass garden", | |
}, | |
{ | |
Name: "query", | |
Description: "random query", | |
}, | |
} | |
commandHandlers := map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ | |
"query": func(s *discordgo.Session, i *discordgo.InteractionCreate) { | |
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | |
Type: discordgo.InteractionResponseChannelMessageWithSource, | |
Data: &discordgo.InteractionResponseData{ | |
Content: "query res", | |
}, | |
}) | |
}, | |
} | |
discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { | |
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { | |
h(s, i) | |
} | |
}) | |
discord.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) { | |
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator) | |
}) | |
defer discord.Close() | |
if err := discord.Open(); err != nil { | |
panic(err) | |
} | |
log.Println("Adding commands...") | |
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) | |
for i, v := range commands { | |
cmd, err := discord.ApplicationCommandCreate(discord.State.User.ID, gid, v) | |
if err != nil { | |
log.Panicf("Cannot create '%v' command: %v", v.Name, err) | |
} else { | |
log.Printf("added: %v", v.Name) | |
} | |
registeredCommands[i] = cmd | |
} | |
fmt.Println("running!") | |
sc := make(chan os.Signal, 1) | |
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) | |
<-sc | |
// for _, v := range registeredCommands { | |
// err := discord.ApplicationCommandDelete(discord.State.User.ID, gid, v.ID) | |
// if err != nil { | |
// log.Panicf("Cannot delete '%v' command: %v", v.Name, err) | |
// } | |
// } | |
} | |
func msg(s *discordgo.Session, m *discordgo.MessageCreate) { | |
if m.Author.ID == s.State.User.ID { | |
return | |
} | |
if m.Content == "ping" { | |
// log.Println(m.Content) | |
s.ChannelMessageSend(m.ChannelID, "Pong!") | |
} | |
// lib.Record(wapi, m.Author.ID, 1, m.Timestamp) | |
log.Printf("save msg @%s: %s\n", m.Author.Username, m.Content) | |
// } | |
if strings.HasPrefix(m.Message.Content, "!query") { | |
q := m.Message.Content[7:] | |
log.Println("query:", q) | |
if res, err := qapi.QueryRaw(context.Background(), q, influxdb2.DefaultDialect()); err != nil { | |
log.Println("err:", err) | |
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("error:\n```%s```", err.Error())) | |
} else { | |
log.Println("result:", res) | |
if _, err := s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("res:\n```%s```", res)); err != nil { | |
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("failed to send message:\n```%s```", err.Error())) | |
} | |
} | |
} | |
if m.Message.Content == "!a" { | |
// q := m.Message.Content[2:] | |
rank := lib.Rank(qapi) | |
var buf bytes.Buffer | |
for k, v := range rank { | |
buf.WriteString(fmt.Sprintf("<@%s>: %d", k, v)) | |
} | |
s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{ | |
Title: "asdf", | |
Description: "<@662201438621138954>", | |
}) | |
// s.ChannelMessageSend("aa") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment