Skip to content

Instantly share code, notes, and snippets.

@gradiuscypher
Created December 29, 2015 20:07
Show Gist options
  • Save gradiuscypher/a831889fd8d221e91ee8 to your computer and use it in GitHub Desktop.
Save gradiuscypher/a831889fd8d221e91ee8 to your computer and use it in GitHub Desktop.
from plugin_loader import PluginLoader
from threading import Thread
from concurrent.futures import ProcessPoolExecutor
import discord
import asyncio
import configparser
import traceback
import json
config = configparser.RawConfigParser()
client = discord.Client()
plugins = PluginLoader()
loop = asyncio.get_event_loop()
execute = ProcessPoolExecutor(2)
def fib(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2)
def test_fun(message, tclient):
split_content = message.content.split()
if split_content[0] == "!f":
print("Yielding")
yield from tclient.send_message(message.channel, "Sleeping.")
print("Sleeping")
print(fib(int(split_content[1])))
print("Yielding")
yield from tclient.send_message(message.channel, "Awake.")
print("Awake")
else:
yield from tclient.send_message(message.channel, message.content)
@client.async_event
def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.async_event
def on_message(message):
server_id = config.get("BotSettings", "server_id")
selfname = config.get("BotSettings", "self_name")
permitted_channels = json.loads(config.get('BotSettings', 'permitted_channels'))
if message.channel.is_private:
# code ommited, as it's not related
else:
if message.server.id == server_id:
if not message.author.name == selfname and message.channel.name in permitted_channels:
for plugin in plugins.public_plugins:
print("trying", repr(plugin), "on", message.content)
try:
# yield from test_fun(message, client)
# yield from plugin.action(message, client)
# asyncio.async(test_fun(message, client))
# asyncio.async(plugin.action(message, client))
# loop.run_in_executor(None, plugin.action, message, client)
# loop.run_in_executor(None, test_fun, message, client)
# loop.create_task(plugin.action(message, client))
# pi = asyncio.async(loop.run_in_executor(execute, test_fun, message, client))
except:
print("There was an error with: " + str(plugin))
print(traceback.format_exc())
def main_task(config_file):
config.read(config_file)
email = config.get("Account", "email")
password = config.get("Account", "password")
plugins.load_plugins(config)
# client.run(email, password)
yield from client.login(email, password)
yield from client.connect()
def start_bot(config_file):
loop.run_until_complete(main_task(config_file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment