Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

package main import ( "encoding/json" "fmt" "github.com/bwmarrin/discordgo" "io/ioutil"…

  1. User

    package main

    import ( "encoding/json" "fmt" "github.com/bwmarrin/discordgo" "io/ioutil" "os" "os/signal" "strings" "syscall" )

    const ( prefix = "+" prefix1 = "-" prefix2 = "!" )

    var ( whiteList map[string]bool allowedChannels map[string]bool )

    func main() { loadJSON("whitelist.json", &whiteList) loadJSON("channels.json", &allowedChannels)

    token := "YOUR_BOT_TOKEN_HERE"
    
    dg, err := discordgo.New("Bot " + token)
    if err != nil {
    	fmt.Println("Error creating Discord session:", err)
    	return
    }
    
    dg.AddHandler(messageCreate)
    
    err = dg.Open()
    if err != nil {
    	fmt.Println("Error opening Discord session:", err)
    	return
    }
    defer dg.Close()
    
    fmt.Println("Bot is now running. Press Ctrl+C to exit.")
    
    sc := make(chan os.Signal, 1)
    signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
    <-sc
    

    }

    func loadJSON(filename string, v interface{}) { file, err := ioutil.ReadFile(filename) if err == nil { _ = json.Unmarshal(file, v) } }

    func saveJSON(filename string, v interface{}) { data, err := json.MarshalIndent(v, "", " ") if err == nil { _ = ioutil.WriteFile(filename, data, 0644) } }

    func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { if m.Author.ID == s.State.User.ID { // Ignore the bot's own messages return }

    if !allowedChannels[m.ChannelID] && !whiteList[m.Author.ID] {
    	return
    }
    
    if strings.HasPrefix(m.Content, prefix) || strings.HasPrefix(m.Content, prefix1) || strings.HasPrefix(m.Content, prefix2) {
    	parts := strings.Fields(m.Content)
    	command := parts[0]
    
    	switch command {
    	case prefix2 + "addwhitechan":
    		if len(parts) < 2 {
    			return
    		}
    
    		allowedChannels[parts[1]] = true
    
    		saveJSON("channels.json", allowedChannels)
    
    		_, err := s.ChannelMessageSend(m.ChannelID, "Channel added to whitelist.")
    		if err != nil {
    			panic(err)
    		}
    
    	case prefix2 + "removechan":
    
    		if len(parts) < 2 {
    			return
    		}
    
    		delete(allowedChannels, parts[1])
    		saveJSON("channels.json", allowedChannels)
    
    		_, err := s.ChannelMessageSend(m.ChannelID, "Channel removed from whitelist.")
    		if err != nil {
    			panic(err)
    		}
    
    	case prefix2 + "addwhitemember":
    
    		if len(parts) < 2 {
    			return
    		}
    
    		whiteList[parts[1]] = true
    		saveJSON("whitelist.json", whiteList)
    
    		_, err := s.ChannelMessageSend(m.ChannelID, "User added to whitelist.")
    		if err != nil {
    			panic(err)
    		}
    
    	case prefix2 + "removewhitemember":
    		if len(parts) < 2 {
    			return
    		}
    
    		delete(whiteList, parts[1])
    		saveJSON("whitelist.json", whiteList)
    
    		_, err := s.ChannelMessageSend(m.ChannelID, "User removed from whitelist.")
    		if err != nil {
    			panic(err)
    		}
    
    	}
    }
    

    }

    добавь дефолтные каналы

    var ( whiteList = map[string]bool{ "1184449624950460488": true, "683970164861042690": true, }

    allowedChannels = map[string]bool{
            "1087629024139943936": true,
            "1174286575878815855": true,
    }
    

    )

  2. Assistant

    You've provided Go code for a Discord bot that maintains a whitelist of users and channels, allowing commands to be processed only from these whitelisted entities. It seems you want to add default whitelisted channels and users directly in the code. Let's integrate these default values into your existing code.

    First, declare whiteList and allowedChannels using the default values at the beginning of the main() function definition. Make sure that your loadJSON() function doesn't override them unless it actually reads from the file. Here's the modified version of your code:

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"github.com/bwmarrin/discordgo"
    	"io/ioutil"
    	"os"
    	"os/signal"
    	"strings"
    	"syscall"
    )
    
    const (
    	prefix  = "+"
    	prefix1 = "-"
    	prefix2 = "!"
    )
    
    var (
    	whiteList = map[string]bool{
    		"1184449624950460488": true,
    		"683970164861042690":  true,
    	}
    
    	allowedChannels = map[string]bool{
    		"1087629024139943936": true,
    		"1174286575878815855": true,
    	}
    )
    
    func main() {
    	// Load existing whitelist and channels JSON files if available. These will overwrite the defaults if the files are present.
    	loadJSON("whitelist.json", &whiteList)
    	loadJSON("channels.json", &allowedChannels)
    
    	token := "YOUR_BOT_TOKEN_HERE"
    
    	dg, err := discordgo.New("Bot " + token)
    	if err != nil {
    		fmt.Println("Error creating Discord session:", err)
    		return
    	}
    
    	dg.AddHandler(messageCreate)
    
    	err = dg.Open()
    	if err != nil {
    		fmt.Println("Error opening Discord session:", err)
    		return
    	}
    	defer dg.Close()
    
    	fmt.Println("Bot is now running. Press Ctrl+C to exit.")
    
    	sc := make(chan os.Signal, 1)
    	signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
    	<-sc
    }
    
    func loadJSON(filename string, v *map[string]bool) {
    	file, err := ioutil.ReadFile(filename)
    	if err == nil {
    		var data map[string]bool
    		err := json.Unmarshal(file, &data)
    		if err == nil {
    			for key, value := range data {
    				(*v)[key] = value
    			}
    		}
    	}
    }
    
    func saveJSON(filename string, v interface{}) {
    	data, err := json.MarshalIndent(v, "", "  ")
    	if err == nil {
    		_ = ioutil.WriteFile(filename, data, 0644)
    	}
    }
    
    func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
    	if m.Author.ID == s.State.User.ID { // Ignore the bot's own messages
    		return
    	}
    
    	if !allowedChannels[m.ChannelID] && !whiteList[m.Author.ID] {
    		return
    	}
    
    	if strings.HasPrefix(m.Content, prefix) || strings.HasPrefix(m.Content, prefix1) || strings.HasPrefix(m.Content, prefix2) {
    		parts := strings.Fields(m.Content)
    		command := parts[0]
    
    		switch command {
    		case prefix2 + "addwhitechan":
    			if len(parts) < 2 {
    				return
    			}
    
    			allowedChannels[parts[1]] = true
    			saveJSON("channels.json", allowedChannels)
    
    			_, err := s.ChannelMessageSend(m.ChannelID, "Channel added to whitelist.")
    			if err != nil {
    				panic(err)
    			}
    
    		case prefix2 + "removechan":
    			if len(parts) < 2 {
    				return
    			}
    
    			delete(allowedChannels, parts[1])
    			saveJSON("channels.json", allowedChannels)
    
    			_, err := s.ChannelMessageSend(m.ChannelID, "Channel removed from whitelist.")
    			if err != nil {
    				panic(err)
    			}
    
    		case prefix2 + "addwhitemember":
    			if len(parts) < 2 {
    				return
    			}
    
    			whiteList[parts[1]] = true
    			saveJSON("whitelist.json", whiteList)
    
    			_, err := s.ChannelMessageSend(m.ChannelID, "User added to whitelist.")
    			if err != nil {
    				panic(err)
    			}
    
    		case prefix2 + "removewhitemember":
    			if len(parts) < 2 {
    				return
    			}
    
    			delete(whiteList, parts[1])
    			saveJSON("whitelist.json", whiteList)
    
    			_, err := s.ChannelMessageSend(m.ChannelID, "User removed from whitelist.")
    			if err != nil {
    				panic(err)
    			}
    		}
    	}
    }
    

    Key Updates:

    1. Default Initialization: The whiteList and allowedChannels maps are initialized with default values before attempting to load additional data from JSON files.

    2. Merging with JSON Data: The loadJSON function now safely adds additional data from the JSON file to the existing map rather than replacing it, allowing added defaults to persist unless explicitly overwritten. This ensures that defaults remain intact unless they are specifically configured differently in the JSON files.

    3. Function Parameter Change: The parameter for the loadJSON function has been changed to a pointer to a map[string]bool to allow in-place modification.