Skip to content

Instantly share code, notes, and snippets.

@Cloudef
Last active November 29, 2023 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cloudef/1195c7150ff139f7c521029ff944f9a0 to your computer and use it in GitHub Desktop.
Save Cloudef/1195c7150ff139f7c521029ff944f9a0 to your computer and use it in GitHub Desktop.
limnoria discord bridge
###
# Copyright (c) 2014, Jari Vetoniemi
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import supybot.conf as conf
import supybot.registry as registry
try:
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization('Discord')
except:
# Placeholder that allows to run the plugin on a bot
# without the i18n module
_ = lambda x:x
def configure(advanced):
# This will be called by supybot to configure this module. advanced is
# a bool that specifies whether the user identified themself as an advanced
# user or not. You should effect your configuration by manipulating the
# registry as appropriate.
from supybot.questions import expect, anything, something, yn
Discord = conf.registerPlugin('Discord', True)
Discord.token.setValue(expect("Discord bot token"))
Discord = conf.registerPlugin('Discord')
# This is where your configuration variables (if any) should go. For example:
conf.registerGlobalValue(Discord, 'token', registry.String('', _("""Discord bot token"""), private=True))
conf.registerGlobalValue(Discord, 'whitelisted', registry.SpaceSeparatedListOfStrings('', _("""Channels the bot listens to""")))
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
###
# Copyright (c) 2014, Jari Vetoniemi
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
"""
Discord bridge
"""
import supybot
import supybot.world as world
# Use this for the version of this plugin. You may wish to put a CVS keyword
# in here if you're keeping the plugin in CVS or some similar system.
__version__ = "0.0.1"
__author__ = supybot.Author('Jari Vetoniemi', 'Cloudef', 'mailroxas@gmail.com')
# This is a dictionary mapping supybot.Author instances to lists of
# contributions.
__contributors__ = {}
# This is a url where the most recent plugin package can be downloaded.
__url__ = ''
from . import config
from . import plugin
from imp import reload
# In case we're being reloaded.
reload(config)
reload(plugin)
# Add more reloads here if you add third-party modules and want them to be
# reloaded when this plugin is reloaded. Don't forget to import them as well!
if world.testing:
from . import test
Class = plugin.Class
configure = config.configure
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
###
# Copyright (c) 2014, Jari Vetoniemi
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import sys
import threading
import asyncio
import discord
from supybot.commands import *
import supybot.callbacks as callbacks
import supybot.ircmsgs as ircmsgs
try:
from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('Discord')
except ImportError:
# Placeholder that allows to run the plugin on a bot
# without the i18n module
_ = lambda x:x
class DiscordClient(discord.Client):
def __init__(self, irc, log, whitelisted=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.irc = irc
self.log = log
self.whitelisted = whitelisted
def is_whitelisted(self, ch):
return not self.whitelisted or ch in self.whitelisted
async def on_ready(self):
self.log.info('Logged into Discord as {0}'.format(self.user))
async def on_message(self, message):
if message.author == self.user or len(message.content) == 0:
return
if hasattr(message.channel, 'recipient'):
msg = ircmsgs.privmsg(self.irc.nick, message.content)
else:
if not self.is_whitelisted(message.channel.name):
return
msg = ircmsgs.privmsg('#{0}'.format(message.channel.name), message.content)
msg.prefix = self.irc.prefix
msg.nick = message.author.name
msg.tag("discord", message)
self.irc.feedMsg(msg)
class Discord(callbacks.Plugin):
"""Discord bridge"""
def __init__(self, irc):
super(Discord, self).__init__(irc)
self.irc = irc
intents = discord.Intents(messages=True, guilds=True, message_content=True)
self.discord = DiscordClient(intents=intents, irc=irc, log=self.log,
whitelisted=self.registryValue('whitelisted'))
def run():
self.discord.run(self.registryValue('token'))
self.thread = threading.Thread(target=run, daemon=True)
self.thread.start()
self.log.info('Discord plugin loaded')
def outFilter(self, irc, msg):
orig = msg.inReplyTo
if orig and orig.discord:
asyncio.run_coroutine_threadsafe(orig.discord.channel.send(msg.args[1]), self.discord.loop)
return None
return msg
def __del__(self):
self.thread.exit()
Class = Discord
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment