Skip to content

Instantly share code, notes, and snippets.

@Sneppys
Last active November 29, 2016 03:54
Show Gist options
  • Save Sneppys/afcda7311f600926013a37fdb34123a0 to your computer and use it in GitHub Desktop.
Save Sneppys/afcda7311f600926013a37fdb34123a0 to your computer and use it in GitHub Desktop.
import discord
import asyncio
#############################################################################
# Configuration #
#############################################################################
# NOTE: you need to install discord.py and have Python 3.5.2
# discord.py: https://github.com/Rapptz/discord.py
# python: https://www.python.org/downloads/ (3.5.2)
# these are the role names that are handled by the bot through commands
# note: the bot needs permission to add and remove these roles
roles_to_handle = {"rolename1", "rolename2"}
# this is the application's token
# get an application token from
# https://discordapp.com/developers/applications/me
app_token = "[TOKEN]"
#############################################################################
# Code #
#############################################################################
client = discord.Client()
async def handleRoleChangeMethod(channel, author, message, role_name):
# Handles the specified role if a user enters its command
if (message.content.lower().startswith('!{}'
.format(role_name.lower()))):
# get role object if command is sent
role = next(x for x in channel.server.roles if x.name == role_name)
if (role):
# if the role exists, check the author's role tags
if (role in author.roles):
# remove role from author if they already had it
await client.remove_roles(author, role)
await client.send_message(channel,
("{} Removed you from {} role!"
" :thumbsup:")
.format(author.mention, role_name))
else:
# add role to author if they didn't have it
await client.add_roles(author, role)
await client.send_message(channel,
"{} Added you to {} role! :thumbsup:"
.format(author.mention, role_name))
async def handleMessage(channel, author, message):
# handle a message in a channel
if (author.server_permissions.administrator):
# only run these commands if sent by an admin
if (message.content.startswith('!py')):
await client.send_message(channel, "Python is running")
# handle the roles specified above
for r in roles_to_handle:
await handleRoleChangeMethod(channel, author, message, r)
@client.event
async def on_ready():
print("Ready!")
print("connected as {}".format((client.user.name, client.user.id)))
@client.event
async def on_message(message):
# message event
# will call handleMessage() if the requirements are met
author = message.author
if (message.type != discord.MessageType.default):
return
if (author.bot):
return
if (message.channel.type != discord.ChannelType.text):
return
permissions = message.channel.permissions_for(message.channel.server.me)
if (not permissions.send_messages):
return
await handleMessage(message.channel, author, message)
# start the bot's connection
client.run(app_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment