ImplDiscordStuff #1
							
								
								
									
										165
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										165
									
								
								main.go
									
									
									
									
									
								
							@@ -7,6 +7,7 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"os/signal"
 | 
						"os/signal"
 | 
				
			||||||
	"syscall"
 | 
						"syscall"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/BurntSushi/toml"
 | 
						"github.com/BurntSushi/toml"
 | 
				
			||||||
	"github.com/bwmarrin/discordgo"
 | 
						"github.com/bwmarrin/discordgo"
 | 
				
			||||||
@@ -26,6 +27,7 @@ type Config struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
type Discord struct {
 | 
					type Discord struct {
 | 
				
			||||||
	Token   string `toml:"client"`
 | 
						Token   string `toml:"client"`
 | 
				
			||||||
 | 
						AppID	string	`toml:"appid"`
 | 
				
			||||||
	GuildID string `toml:"guildid"`
 | 
						GuildID string `toml:"guildid"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -52,7 +54,6 @@ func connectDb(config Config) (*sql.DB, error) {
 | 
				
			|||||||
		config.Database.Password,
 | 
							config.Database.Password,
 | 
				
			||||||
		config.Database.Name,
 | 
							config.Database.Name,
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	log.Println(connectionString)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	db, err := sql.Open("postgres", connectionString)
 | 
						db, err := sql.Open("postgres", connectionString)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@@ -90,6 +91,149 @@ func init() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func searchLink(message, format, sep string) string {
 | 
				
			||||||
 | 
						return fmt.Sprintf(format, strings.Join(
 | 
				
			||||||
 | 
							strings.Split(
 | 
				
			||||||
 | 
								message,
 | 
				
			||||||
 | 
								" ",
 | 
				
			||||||
 | 
							),
 | 
				
			||||||
 | 
							sep,
 | 
				
			||||||
 | 
						))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						commands = []discordgo.ApplicationCommand{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name: "rickroll-em",
 | 
				
			||||||
 | 
								Type: discordgo.UserApplicationCommand,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name: "google-it",
 | 
				
			||||||
 | 
								Type: discordgo.MessageApplicationCommand,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name: "stackoverflow-it",
 | 
				
			||||||
 | 
								Type: discordgo.MessageApplicationCommand,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name: "godoc-it",
 | 
				
			||||||
 | 
								Type: discordgo.MessageApplicationCommand,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name: "discordjs-it",
 | 
				
			||||||
 | 
								Type: discordgo.MessageApplicationCommand,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								Name: "discordpy-it",
 | 
				
			||||||
 | 
								Type: discordgo.MessageApplicationCommand,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						commandsHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
 | 
				
			||||||
 | 
							"rickroll-em": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
 | 
				
			||||||
 | 
								err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
 | 
				
			||||||
 | 
									Type: discordgo.InteractionResponseChannelMessageWithSource,
 | 
				
			||||||
 | 
									Data: &discordgo.InteractionResponseData{
 | 
				
			||||||
 | 
										Content: "Operation rickroll has begun",
 | 
				
			||||||
 | 
										Flags:   discordgo.MessageFlagsEphemeral,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								ch, err := s.UserChannelCreate(
 | 
				
			||||||
 | 
									i.ApplicationCommandData().TargetID,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									_, err = s.FollowupMessageCreate(i.Interaction, true, &discordgo.WebhookParams{
 | 
				
			||||||
 | 
										Content: fmt.Sprintf("Mission failed. Cannot send a message to this user: %q", err.Error()),
 | 
				
			||||||
 | 
										Flags:   discordgo.MessageFlagsEphemeral,
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										panic(err)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								_, err = s.ChannelMessageSend(
 | 
				
			||||||
 | 
									ch.ID,
 | 
				
			||||||
 | 
									fmt.Sprintf("%s sent you this: https://youtu.be/dQw4w9WgXcQ", i.Member.Mention()),
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"google-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
 | 
				
			||||||
 | 
								err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
 | 
				
			||||||
 | 
									Type: discordgo.InteractionResponseChannelMessageWithSource,
 | 
				
			||||||
 | 
									Data: &discordgo.InteractionResponseData{
 | 
				
			||||||
 | 
										Content: searchLink(
 | 
				
			||||||
 | 
											i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
 | 
				
			||||||
 | 
											"https://google.com/search?q=%s", "+"),
 | 
				
			||||||
 | 
										Flags: discordgo.MessageFlagsEphemeral,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"stackoverflow-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
 | 
				
			||||||
 | 
								err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
 | 
				
			||||||
 | 
									Type: discordgo.InteractionResponseChannelMessageWithSource,
 | 
				
			||||||
 | 
									Data: &discordgo.InteractionResponseData{
 | 
				
			||||||
 | 
										Content: searchLink(
 | 
				
			||||||
 | 
											i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
 | 
				
			||||||
 | 
											"https://stackoverflow.com/search?q=%s", "+"),
 | 
				
			||||||
 | 
										Flags: discordgo.MessageFlagsEphemeral,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"godoc-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
 | 
				
			||||||
 | 
								err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
 | 
				
			||||||
 | 
									Type: discordgo.InteractionResponseChannelMessageWithSource,
 | 
				
			||||||
 | 
									Data: &discordgo.InteractionResponseData{
 | 
				
			||||||
 | 
										Content: searchLink(
 | 
				
			||||||
 | 
											i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
 | 
				
			||||||
 | 
											"https://pkg.go.dev/search?q=%s", "+"),
 | 
				
			||||||
 | 
										Flags: discordgo.MessageFlagsEphemeral,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"discordjs-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
 | 
				
			||||||
 | 
								err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
 | 
				
			||||||
 | 
									Type: discordgo.InteractionResponseChannelMessageWithSource,
 | 
				
			||||||
 | 
									Data: &discordgo.InteractionResponseData{
 | 
				
			||||||
 | 
										Content: searchLink(
 | 
				
			||||||
 | 
											i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
 | 
				
			||||||
 | 
											"https://discord.js.org/#/docs/main/stable/search?query=%s", "+"),
 | 
				
			||||||
 | 
										Flags: discordgo.MessageFlagsEphemeral,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							"discordpy-it": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
 | 
				
			||||||
 | 
								err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
 | 
				
			||||||
 | 
									Type: discordgo.InteractionResponseChannelMessageWithSource,
 | 
				
			||||||
 | 
									Data: &discordgo.InteractionResponseData{
 | 
				
			||||||
 | 
										Content: searchLink(
 | 
				
			||||||
 | 
											i.ApplicationCommandData().Resolved.Messages[i.ApplicationCommandData().TargetID].Content,
 | 
				
			||||||
 | 
											"https://discordpy.readthedocs.io/en/stable/search.html?q=%s", "+"),
 | 
				
			||||||
 | 
										Flags: discordgo.MessageFlagsEphemeral,
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									panic(err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
	if client == nil {
 | 
						if client == nil {
 | 
				
			||||||
		log.Println("Bot client is not initialized")
 | 
							log.Println("Bot client is not initialized")
 | 
				
			||||||
@@ -100,6 +244,25 @@ func main() {
 | 
				
			|||||||
		log.Println("Bot is online")
 | 
							log.Println("Bot is online")
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						client.AddHandler(func(client *discordgo.Session, i *discordgo.InteractionCreate) {
 | 
				
			||||||
 | 
							if h, ok := commandsHandlers[i.ApplicationCommandData().Name]; ok {
 | 
				
			||||||
 | 
								h(client, i)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cmdIDs := make(map[string]string, len(commands))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, cmd := range commands {
 | 
				
			||||||
 | 
							rcmd, err := client.ApplicationCommandCreate(config.Discord.AppID, config.Discord.GuildID, &cmd)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Fatalf("Cannot create slash command %q: %v", cmd.Name, err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							cmdIDs[rcmd.ID] = rcmd.Name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := client.Open()
 | 
						err := client.Open()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Println("Error opening connection:", err)
 | 
							log.Println("Error opening connection:", err)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user