Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Feat (utils): hidding add & del roles methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumesover committed Apr 19, 2019
1 parent 6490085 commit 8f6f82c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.venv/
__pycache__
config.yml
test-config.yml
14 changes: 6 additions & 8 deletions README.md
Expand Up @@ -4,21 +4,19 @@ This is a discord bot that allow you to authentificate users using their emails.

### Run this bot localy

/!\ The API is not working with Python > 3.6 /!\

```bash
# First get a postgresql server running on your computer
$ docker run --rm -p 5432:5432 postgres

# Edit database url in config file
url: postgres://postgres@localhost/postgres
# url: postgres://postgres@localhost/postgres
$ $EDITOR config.yml

# The project run on python3.6, so stay explicit
python3.6 -m venv .venv
source .venv/bin/activate
python3.6 -m pip install -r requirements.txt
$ python3 -m venv .venv
$ source .venv/bin/activate
$ python3 -m pip install -r requirements.txt

# Simply run the bot by doing
python3.6 main.py
python main.py
```

20 changes: 10 additions & 10 deletions admin.py
Expand Up @@ -11,16 +11,16 @@ async def new_message(Bot, message, config):
msg = message.content.split(' ')

async def help():
msg = """```
Usage:
- help show this message
- new [*users | @everyone] send hello message or update
- update [*users | @everyone] update roles of user
- this get the server page on website
- get [*users | id | login] get the member page on website
- logout shut down the bot
- syncconf [this | all] reload configuration from server
```"""
msg = (
"```Usage:\n"
"- help show this message\n"
"- new [*users | @everyone] send hello message or update\n"
"- update [*users | @everyone] update roles of user\n"
"- get [*users | id | login] get the member page on website\n"
"- this get the server page on website\n"
"- logout shut down the bot\n"
"- syncconf [this | all] reload configuration from server```"
)
await message.channel.send(msg)

async def update(create_if_unk=False):
Expand Down
8 changes: 4 additions & 4 deletions api.py
Expand Up @@ -24,8 +24,6 @@ def fetch_paginate(next):

data = r.json()

# print('Data recieved from', next, '', data)

output += data['results']
next = data['next']

Expand Down Expand Up @@ -140,10 +138,12 @@ def update_conf(config, server_id):
return True

def on_member_remove(config, guild_id, member_id):
pass
url = "{}/api/members/{}/server/".format(config['website']['url'], member_id)
r = requests.delete(url, {'id': guild_id})

def on_member_join(config, guild_id, member_id):
pass
url = "{}/api/members/{}/server/".format(config['website']['url'], member_id)
r = requests.post(url, {'id': guild_id})

def on_guild_join(config, guild_id):
url = "{}/api/servers/".format(config['website']['url'])
Expand Down
6 changes: 0 additions & 6 deletions main.py
Expand Up @@ -20,7 +20,6 @@
@Bot.event
async def on_ready():
for guild in Bot.guilds:
# print("Connected to:", guild)
api.on_guild_join(config, guild.id)
api.update_conf_all(config)

Expand All @@ -33,11 +32,6 @@ async def on_message(message):

guild_id = message.guild.id

# print(config)
# print(config['servers'])
# print(not guild_id in config['servers'])
# print(config['servers'][guild_id]['is_active'])

if (not guild_id in config['servers']) or not config['servers'][guild_id]['is_active']:
print(message.guild, 'is not active')
return
Expand Down
9 changes: 4 additions & 5 deletions utils.py
Expand Up @@ -4,7 +4,7 @@

import api

async def del_roles(member, roles):
async def __del_roles(member, roles):
if member and roles:
server = member.guild
to_remove = []
Expand All @@ -16,7 +16,7 @@ async def del_roles(member, roles):
# TODO: handle errors (cf https://discordpy.readthedocs.io/en/rewrite/api.html#discord.Member.add_roles)
await member.remove_roles(*to_remove)

async def give_roles(member, roles):
async def __add_roles(member, roles):
if member and roles:
server = member.guild
new_roles = []
Expand Down Expand Up @@ -44,8 +44,8 @@ async def set_roles(config, member, to_set):
to_remove = [id for id in to_del if id in user_roles]
to_add = [id for id in to_set if not id in user_roles]

await del_roles(member, to_remove)
await add_roles(member, to_add)
await __del_roles(member, to_remove)
await __add_roles(member, to_add)

async def send_hello(client, member, hash, config):
try:
Expand Down Expand Up @@ -95,7 +95,6 @@ async def on_member_join(client, member, config, create_if_unk=True):
else:
ranks += config_ranks['confirmed']

# await give_roles(client, config, member, ranks)
await set_roles(config, member, ranks)

def check_ban(member, login, groups, config):
Expand Down

0 comments on commit 8f6f82c

Please sign in to comment.