Skip to content
Snippets Groups Projects
Commit 2b9e2af4 authored by Dnns's avatar Dnns
Browse files

Created fernuni-bot that can add or remove a members roles.

parents
Branches
No related tags found
No related merge requests found
# Django #
*.log
*.pot
*.pyc
__pycache__
db.sqlite3
media
# Backup files #
*.bak
# If you are using PyCharm #
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/gradle.xml
.idea/**/libraries
*.iws /out/
# Python #
*.py[cod]
*$py.class
# Distribution / packaging
.Python build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
.pytest_cache/
nosetests.xml
coverage.xml
*.cover
.hypothesis/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery
celerybeat-schedule.*
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# mkdocs documentation
/site
# mypy
.mypy_cache/
# Sublime Text #
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache
*.sublime-workspace
*.sublime-project
# sftp configuration file
sftp-config.json
# Package control specific files Package
Control.last-run
Control.ca-list
Control.ca-bundle
Control.system-ca-bundle
GitHub.sublime-settings
# Visual Studio Code #
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history
# Default ignored files
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8 (fernuni-bot)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (fernuni-bot)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/fernuni-bot.iml" filepath="$PROJECT_DIR$/.idea/fernuni-bot.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import os
import discord
from dotenv import load_dotenv
# .env file is necessary in the same directory, that contains Token and guild.
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = int(os.getenv('DISCORD_GUILD'))
client = discord.Client()
# Returns an guild object, that matches the id specified in GUILD.
# This guild is the FU Hagen Informatik/Mathematik guild.
def get_guild():
for guild in client.guilds:
if guild.id == GUILD:
return guild
return None
# Get the key for a given role. This role is used for adding or removing a role from a user.
def get_key(role):
if role.name == "B.Sc. Informatik":
return "BI"
elif role.name == "B.Sc. Mathematik":
return "BM"
elif role.name == "B.Sc. Wirtschaftsinformatik":
return "BWI"
elif role.name == "M.Sc. Informatik":
return "MI"
elif role.name == "M.Sc. Praktische Informatik":
return "MPI"
elif role.name == "M.Sc. Mathematik":
return "MM"
elif role.name == "M.Sc. Wirtschaftsinformatik":
return "MWI"
# Get all roles that are available at the guild.
def get_guild_roles():
guild = get_guild()
if guild is not None:
roles = {}
for role in guild.roles:
role_key = get_key(role)
if role_key is not None:
roles[role_key] = role
return roles
return None
# Get all roles assigned to a member.
def get_members_roles(user):
guild = get_guild()
if type(user) is discord.User and guild is not None:
return guild.get_member(user.id).roles
return None
# Send DM to a user/member
async def send_dm(user, message):
if type(user) is discord.User or type(user) is discord.Member:
if user.dm_channel is None:
await user.create_dm()
await user.dm_channel.send(message)
# Send help message as DM
async def fu_help(message):
answer = f'Ich bin der Fernuni-Bot. Ich kann Rollen zuweisen/entfernen. \n\n'
answer += f'Folgende Kommandos stehen zur verfügung:\n'
answer += f'`=fu-help` - Zeigt diesen Hilfetext an\n'
answer += f'`=fu-all-roles` - Zeigt alle verfügbaren Rollen an, die ich dir zuweisen/entfernen kann.\n'
answer += f'`=fu-my-roles` - Zeigt alle Rollen an, die dir momentan zugewiesen sind.\n'
answer += f'`=fu-add-roles` - Mit diesem Kommando, gefolgt von einer Liste von Rollen, weise ich dir diese Rollen zu.\n'
answer += f'`=fu-remove roles` - Mit diesem Kommendo, gefolgt von einer Liste von Rollen, entferne ich dir diese Rollen.\n\n'
answer += f'Hinweise für die Nutzung der Kommandos zum Zuweisen/Entfernen von Rollen:\n'
answer += f'In der Liste der verfügbaren Rollen siehst du in eckigen Klammern einen Schlüssel für die jeweilige Rolle, die für diese Kommandos zu verwenden ist. \n'
answer += f'Beispiel: \n[BI] B.Sc. Informatik \n[MM] M.Sc. Mathematik \n'
answer += f'`=fu-add-roles BI` fügt die Rolle B.Sc. Informatik hinzu. \n'
answer += f'`=fu-remove-roles BI MM` entfernt die Rollen B.Sc. Informatik und M.Sc Mathematik. \n'
await send_dm(message.author, answer)
# Send all available roles that can be assigned to a member by this bot as DM
async def fu_all_roles(message):
roles = get_guild_roles()
answer = "Verfügbare Rollen: \n"
for key, role in roles.items():
answer += f'[{key}] {role.name}\n'
await send_dm(message.author, answer)
# Send the roles assigned to a member as DM.
async def fu_my_roles(message):
my_roles = get_members_roles(message.author)
answer = "Dir zugewiesene Rollen:\n"
if my_roles is not None:
for role in my_roles:
key = get_key(role)
if key is not None:
answer += f'[{key}] {role.name} \n'
await send_dm(message.author, answer)
# Add or remove roles assigned to a member. Multiple roles can be added with one command, or removed.
async def fu_modify_roles(message, add):
msg = message.content
splitted_msg = msg.split(" ")
guild = get_guild()
if guild is not None:
member = guild.get_member(message.author.id)
if len(splitted_msg) > 1:
roles = get_guild_roles()
for key in splitted_msg[1:]:
if key in roles:
role = roles[key]
if add:
try:
await member.add_roles(role)
await send_dm(message.author, f'Dir wurde die Rolle {role.name} hinzugefügt')
except Exception:
await send_dm(message.author, f'Fehler bei der Zuweisung der Rolle {role.name}')
else:
try:
await member.remove_roles(role)
await send_dm(message.author, f'Dir wurde die Rolle {role.name} entfernt')
except Exception:
await send_dm(message.author, f'Fehler bei der Entfernung der Rolle {role.name}')
@client.event
async def on_ready():
print("Client started!")
@client.event
async def on_message(message):
msg = message.content
if message.author == client.user:
return
if msg.startswith("=fu-help"):
await fu_help(message)
elif msg.startswith("=fu-all-roles"):
await fu_all_roles(message)
elif msg.startswith("=fu-my-roles"):
await fu_my_roles(message)
elif msg.startswith("=fu-add-roles"):
await fu_modify_roles(message, add=True)
elif msg.startswith("=fu-remove-roles"):
await fu_modify_roles(message, add=False)
client.run(TOKEN)
aiohttp==3.6.2
async-timeout==3.0.1
attrs==19.3.0
chardet==3.0.4
discord==1.0.1
discord.py==1.3.3
idna==2.9
multidict==4.7.5
python-dotenv==0.12.0
websockets==8.1
yarl==1.4.2
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment