Revert "now added global commands" (DMs can crash the bot currently)
This commit is contained in:
40
handlers.go
40
handlers.go
@@ -28,27 +28,38 @@ func ready(s *discordgo.Session, event *discordgo.Ready) {
|
|||||||
findAndDeleteUnusedMessages()
|
findAndDeleteUnusedMessages()
|
||||||
removeOldCommandFromAllGuilds(s)
|
removeOldCommandFromAllGuilds(s)
|
||||||
var existingCommandNames []string
|
var existingCommandNames []string
|
||||||
existingCommands, err := s.ApplicationCommands(s.State.User.ID, "")
|
for _, guild := range event.Guilds {
|
||||||
if err != nil {
|
existingCommands, err := s.ApplicationCommands(s.State.User.ID, guild.ID)
|
||||||
logrus.Errorf("error fetching existing global commands: %v", err)
|
|
||||||
} else {
|
|
||||||
for _, existingCommand := range existingCommands {
|
for _, existingCommand := range existingCommands {
|
||||||
existingCommandNames = append(existingCommandNames, existingCommand.Name)
|
existingCommandNames = append(existingCommandNames, existingCommand.Name)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("error fetching existing commands for guild %s: %v", guild.Name, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
if !slices.Contains(existingCommandNames, command.Definition.Name) || slices.Contains(os.Args, "--update="+command.Definition.Name) || slices.Contains(os.Args, "--update=all") {
|
if !slices.Contains(existingCommandNames, command.Definition.Name) || slices.Contains(os.Args, "--update="+command.Definition.Name) {
|
||||||
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", &command.Definition)
|
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, guild.ID, &command.Definition)
|
||||||
|
logrus.Infof("Added command '%s'", cmd.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error creating global command '%s': %v", cmd.Name, err)
|
logrus.Error("error creating command,", err)
|
||||||
} else {
|
continue
|
||||||
logrus.Infof("Added global command '%s'", cmd.Name)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logrus.Info("Successfully started the Bot!")
|
logrus.Info("Successfully started the Bot!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
|
||||||
|
for _, command := range commands {
|
||||||
|
_, err := s.ApplicationCommandCreate(s.State.User.ID, event.Guild.ID, &command.Definition)
|
||||||
|
if err != nil {
|
||||||
|
logrus.Errorf("error creating command for guild %s: %v", event.Guild.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
switch i.Type {
|
switch i.Type {
|
||||||
@@ -92,9 +103,12 @@ func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func removeOldCommandFromAllGuilds(s *discordgo.Session) {
|
func removeOldCommandFromAllGuilds(s *discordgo.Session) {
|
||||||
existingCommands, err := s.ApplicationCommands(s.State.User.ID, "")
|
for _, guild := range s.State.Guilds {
|
||||||
|
existingCommands, err := s.ApplicationCommands(s.State.User.ID, guild.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error fetching existing commands: %v\n", err)
|
logrus.Errorf("error fetching existing commands for guild %s: %v\n", guild.Name, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
var commandIDs []string
|
var commandIDs []string
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
commandIDs = append(commandIDs, command.Definition.Name)
|
commandIDs = append(commandIDs, command.Definition.Name)
|
||||||
@@ -102,9 +116,9 @@ func removeOldCommandFromAllGuilds(s *discordgo.Session) {
|
|||||||
for _, existingCommand := range existingCommands {
|
for _, existingCommand := range existingCommands {
|
||||||
if !slices.Contains(commandIDs, existingCommand.Name) {
|
if !slices.Contains(commandIDs, existingCommand.Name) {
|
||||||
logrus.Infof("Deleting command '%s'", existingCommand.Name)
|
logrus.Infof("Deleting command '%s'", existingCommand.Name)
|
||||||
err := s.ApplicationCommandDelete(s.State.User.ID, "", existingCommand.ID)
|
err := s.ApplicationCommandDelete(s.State.User.ID, guild.ID, existingCommand.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("error deleting command %s: %v", existingCommand.Name, err)
|
logrus.Errorf("error deleting command %s for guild %s: %v", existingCommand.Name, guild.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1
main.go
1
main.go
@@ -42,6 +42,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
bot.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentMessageContent | discordgo.IntentGuildMembers
|
bot.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuilds | discordgo.IntentMessageContent | discordgo.IntentGuildMembers
|
||||||
bot.AddHandler(ready)
|
bot.AddHandler(ready)
|
||||||
|
bot.AddHandler(guildCreate)
|
||||||
bot.AddHandler(interactionCreate)
|
bot.AddHandler(interactionCreate)
|
||||||
bot.AddHandler(messageCreate)
|
bot.AddHandler(messageCreate)
|
||||||
bot.AddHandler(messageDelete)
|
bot.AddHandler(messageDelete)
|
||||||
|
Reference in New Issue
Block a user