-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (87 loc) · 2.53 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
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/profiluefter/IRCord/irc"
"os"
)
func main() {
discordToken := os.Getenv("DISCORD_TOKEN")
guildID := os.Getenv("GUILD_ID")
discord, err := discordgo.New("Bot " + discordToken)
if err != nil {
panic(err.Error())
}
discord.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsGuildMessages)
err = discord.Open()
if err != nil {
panic(err.Error())
}
err = discord.UpdateStatus(0, "")
if err != nil {
panic(err.Error())
}
channels, err := discord.GuildChannels(guildID)
if err != nil {
panic(err.Error())
}
var motd = "This is the message of the day!\nIf you can see this then the server did not crash yet\nNice."
options := irc.ServerOptions{
Name: "irc-cord",
Port: 6667,
Motd: &motd,
}
server := irc.NewServer(options)
var discordIDToIRCChannel = map[string]*irc.Channel{}
var ircChannelToWebhook = map[string]*discordgo.Webhook{}
for _, discordChannel := range channels {
discordChannel := discordChannel
if discordChannel.Type != discordgo.ChannelTypeGuildText {
continue
}
fmt.Printf("Adding channel %s with topic \"%s\"!\n", discordChannel.Name, discordChannel.Topic)
channel := server.NewChannel("#"+discordChannel.Name, discordChannel.Topic)
webhooks, err := discord.ChannelWebhooks(discordChannel.ID)
if err != nil {
panic(err.Error())
}
var channelWebhook *discordgo.Webhook
if len(webhooks) > 0 {
channelWebhook = webhooks[0]
} else {
webhook, err := discord.WebhookCreate(discordChannel.ID, "IRCord", "")
if err != nil {
panic(err.Error())
}
channelWebhook = webhook
}
ircChannelToWebhook[channel.Name] = channelWebhook
listener := func(event irc.Event) {
mre := event.(irc.MessageReceivedEvent)
webhook := ircChannelToWebhook[channel.Name]
_, _ = discord.WebhookExecute(webhook.ID, webhook.Token, false, &discordgo.WebhookParams{
Content: mre.Content,
Username: mre.Nickname,
})
}
channel.AddListener((*irc.EventListener)(&listener))
discordIDToIRCChannel[discordChannel.ID] = channel
}
discord.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return //Ignored
}
if m.GuildID != guildID {
panic(fmt.Sprintf("Message received from unknown server: %s", m.GuildID))
}
channel := discordIDToIRCChannel[m.ChannelID]
if channel == nil {
panic(fmt.Sprintf("Unknown channel %s", m.ChannelID))
}
channel.SendMessage(m.Author.Username, m.Content)
})
err = server.Start()
if err != nil {
fmt.Println(err)
}
}