Skip to content

Instantly share code, notes, and snippets.

@Vocaned
Last active June 29, 2025 08:55
Show Gist options
  • Select an option

  • Save Vocaned/3082d31d150d1cd3ce4df9dc5f449836 to your computer and use it in GitHub Desktop.

Select an option

Save Vocaned/3082d31d150d1cd3ce4df9dc5f449836 to your computer and use it in GitHub Desktop.
import znc
import requests
import re
class zncdiscord(znc.Module):
description = 'Discord webhooks for ZNC'
module_types = [znc.CModInfo.UserModule]
# Never send notifications from nicks in this list
nick_blacklist = ['',]
# Send notifications for all messages from channels in this list
channel_list = ['',]
# Send notifications for all words matched in this list, case insensitive
match_list = ['',]
# If channel is explicitly added, value for that is used. Otherwise * is used
# <nick> is replaced by the nickname
# <channel> is replaced by the channel name
# <network> is replaced by the network name
# <message> is replaced by the message content
webhook_content_map = {
'#example-channel': '<message>',
'*': '[**<nick>** | <channel>@<network>] <message>'
}
webhook_url_map = {
'#example-channel': '[webhookurl1]',
'*': '[webhookurl2]'
}
def escapeDiscord(self, string):
return string.replace('*', '\\*').replace('_', '\\_').replace('~', '\\~').replace('|', '\\|').replace('`', '\\`')
def sendNotification(self, nick, channel, message, network):
content = self.webhook_content_map.get(channel, self.webhook_content_map['*'])
url = self.webhook_url_map.get(channel, self.webhook_url_map['*'])
content = content.replace('<nick>', self.escapeDiscord(nick)).replace('<channel>', self.escapeDiscord(channel)).replace('<network>', self.escapeDiscord(network)).replace('<message>', self.escapeDiscord(message))
data = {
'content': content
}
requests.post(url, data=data)
return znc.CONTINUE
def OnChanMsg(self, nick, channel, message):
regex = re.compile("\x03(?:\\d{1,2}(?:,\\d{1,2})?)?", re.UNICODE)
message = regex.sub("", message.s)
nick = nick.GetNick()
channel = channel.GetName()
network = self.GetNetwork().GetName()
if nick.lower() in self.nick_blacklist:
return znc.CONTINUE
if channel.lower() in self.channel_list:
return self.sendNotification(nick, channel, message, network)
for match in self.match_list:
if match in message.lower():
return self.sendNotification(nick, channel, message, network)
return znc.CONTINUE
def OnChanAction(self, nick, channel, message):
message.s = f'* {message.s}'
return self.OnChanMsg(nick, channel, message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment