Last active
March 31, 2020 15:49
-
-
Save newmania/eb1d410544e291309dad5420f09923bb to your computer and use it in GitHub Desktop.
simple bot that runs perspective API checks on Discord messages and adds reacts if they score high
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord, json, requests | |
from ratelimit import limits | |
#set up Discord client | |
client = discord.Client() | |
#rate limit decorator because Perspective API limits you to 1 query per second | |
@limits(calls=1, period=1) | |
def toxic_check(message): | |
api_key = 'YOUR_KEY_HERE' | |
url = ('https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze' + '?key=' + api_key) | |
#Ask Perspective to analyize for "attacks on identity." Other values such as TOXICITY can be found in their docs | |
data_dict = { | |
'comment': {'text': message}, | |
'languages': ['en'], | |
'requestedAttributes': {'IDENTITY_ATTACK': {}} | |
} | |
response = requests.post(url=url, data=json.dumps(data_dict)) | |
response_dict = json.loads(response.content) | |
attack = response_dict["attributeScores"]["IDENTITY_ATTACK"]["summaryScore"]["value"] | |
return attack | |
@client.event | |
async def on_ready(): | |
print('We have logged in as {0.user}'.format(client)) | |
@client.event | |
async def on_message(message): | |
if message.author == client.user: | |
return | |
#put the check in a try in case we hit the rate limit. Exceeding the rate limit will just drop the check and no react will get added | |
try: | |
if toxic_check(message.clean_content) > 0.9: #0.9 is near the max. I had it at 0.7 and people got annoyed | |
await message.add_reaction("🏳️🌈") | |
except: | |
pass | |
client.run('DISCORD_BOT_KEY') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
discord.py | |
requests | |
ratelimit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment