-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
126 lines (110 loc) · 3.73 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Main package.
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"github.com/bunkercoin/bunkerbot-go/bkc"
"github.com/bwmarrin/discordgo"
)
var apiFlag = flag.String("a", "https://bkcexplorer.bunker.mt/api/", "The Bunkercoin API that should be used.")
// help string for the '.help' command
var helpCmd = `
# Bunkerbot Help:
* **.help** - show a list of commands
* **.bkcblocks** - show the number of blocks in the Bunkercoin Blockchain
* **.bkchashrate** - show the current network hashrate
* **.bkcdiff** - show the current network difficulty
* **.bkcinfo** - show chain information(Blockcount, hashrate, difficulty)
`
// Set the bot up.
func main() {
flag.Parse()
if os.Getenv("BOT_TOKEN") == "" {
log.Fatal("BOT_TOKEN Environment Variable is not set!")
}
dg, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
if err != nil {
log.Fatal(err)
}
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
log.Fatal(err)
}
// Wait here until CTRL-C or another term signal is received.
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
dg.Close()
}
// Helper function that creates the chaininfo embed
func createChainEmbed(chaininfo bkc.ChainInfo) *discordgo.MessageEmbed {
embed := NewEmbed().
SetTitle("Bunkercoin Chain Information").
SetDescription("Info about the Bunkercoin Blockchain").
AddField("Number of blocks", fmt.Sprintf("**%d**", chaininfo.BlockCount)).
AddField("Network Hashrate", fmt.Sprintf("**%.2f**", chaininfo.Hashrate/1000)).
AddField("Network Difficulty", fmt.Sprintf("**%.2f**", chaininfo.Difficulty)).
// SetImage("https://cdn.discordapp.com/avatars/119249192806776836/cc32c5c3ee602e1fe252f9f595f9010e.jpg?size=2048").
// SetThumbnail("https://cdn.discordapp.com/avatars/119249192806776836/cc32c5c3ee602e1fe252f9f595f9010e.jpg?size=2048").
SetColor(0x77c277).MessageEmbed
return embed
}
// Handle MessageCreate events
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
if cmd, ok := strings.CutPrefix(m.Content, "."); ok {
switch cmd {
case "":
break
case "help":
s.ChannelMessageSend(m.ChannelID, helpCmd)
case "bkcblocks":
blockcount, err := bkc.GetBlockCount(*apiFlag)
if err != nil {
log.Println(err)
s.ChannelMessageSend(m.ChannelID, "Error while getting the number of blocks!")
break
}
s.ChannelMessageSend(m.ChannelID, "Number of blocks in the Bunkercoin Blockchain: "+strconv.Itoa(blockcount))
case "bkchashrate":
hashrate, err := bkc.GetHashrate(*apiFlag)
if err != nil {
log.Println(err)
s.ChannelMessageSend(m.ChannelID, "Error while getting the network hashrate!")
break
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Current network hashrate: %.2f KH/s", hashrate/1000))
case "bkcdiff":
diff, err := bkc.GetHashrate(*apiFlag)
if err != nil {
log.Println(err)
s.ChannelMessageSend(m.ChannelID, "Error while getting the network difficulty!")
break
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("Current network difficulty: %.2f", diff))
case "bkcinfo":
chaininfo, err := bkc.GetChainInfo(*apiFlag)
if err != nil {
log.Println(err)
s.ChannelMessageSend(m.ChannelID, "Error while getting Bunkercoin Chain Information!")
break
}
s.ChannelMessageSendEmbed(m.ChannelID, createChainEmbed(chaininfo))
default:
s.ChannelMessageSend(m.ChannelID, "Invalid Command! Type .help to get a list of commands!")
}
}
}