Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code change for discord 2.0.0 migration #37

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
.vscode
# IDE configuration folder
.idea
.vscode

# Logs
logs/logbooks

# Secret files
env

__pycache__/
env/
logs/logbooks/
# Python environement
__pycache__
venv
75 changes: 2 additions & 73 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -74,85 +74,14 @@ confidence=
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use "--disable=all --enable=classes
# --disable=W".
disable=print-statement,
parameter-unpacking,
unpacking-in-except,
old-raise-syntax,
backtick,
long-suffix,
old-ne-operator,
old-octal-literal,
import-star-module-level,
non-ascii-bytes-literal,
raw-checker-failed,
disable=raw-checker-failed,
bad-inline-option,
locally-disabled,
file-ignored,
suppressed-message,
useless-suppression,
deprecated-pragma,
use-symbolic-message-instead,
apply-builtin,
basestring-builtin,
buffer-builtin,
cmp-builtin,
coerce-builtin,
execfile-builtin,
file-builtin,
long-builtin,
raw_input-builtin,
reduce-builtin,
standarderror-builtin,
unicode-builtin,
xrange-builtin,
coerce-method,
delslice-method,
getslice-method,
setslice-method,
no-absolute-import,
old-division,
dict-iter-method,
dict-view-method,
next-method-called,
metaclass-assignment,
indexing-exception,
raising-string,
reload-builtin,
oct-method,
hex-method,
nonzero-method,
cmp-method,
input-builtin,
round-builtin,
intern-builtin,
unichr-builtin,
map-builtin-not-iterating,
zip-builtin-not-iterating,
range-builtin-not-iterating,
filter-builtin-not-iterating,
using-cmp-argument,
eq-without-hash,
div-method,
idiv-method,
rdiv-method,
exception-message-attribute,
invalid-str-codec,
sys-max-int,
bad-python3-import,
deprecated-string-function,
deprecated-str-translate-call,
deprecated-itertools-function,
deprecated-types-field,
next-method-defined,
dict-items-not-iterating,
dict-keys-not-iterating,
dict-values-not-iterating,
deprecated-operator-function,
deprecated-urllib-function,
xreadlines-attribute,
deprecated-sys-function,
exception-escape,
comprehension-escape
use-symbolic-message-instead

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
64 changes: 37 additions & 27 deletions cogs/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
from typing import Optional

import aiohttp
from bs4 import BeautifulSoup
import discord
from bs4 import BeautifulSoup
from discord.ext import commands

from emoji import UNICODE_EMOJI

from src.utils import user_only


# Supported action commands with they template.
# {author} -> command author's mention
# {target} -> user pinged in the command
Expand All @@ -22,7 +19,7 @@
"cuddle": "{author} cuddles {target} :heart:",
"poke": "Hey {target}! {author} poked you",
"baka": "{target} BAKA",
"slap": "{author} slapped {target}!"
"slap": "{author} slapped {target}!",
}


Expand Down Expand Up @@ -52,27 +49,32 @@ async def action(self, ctx, member: discord.User):

# Make a request to the nekos.life API at the command's name endpoint.
async with aiohttp.ClientSession() as session:
async with session.get(f"https://nekos.life/api/v2/img/{ctx.invoked_with}") as response:
async with session.get(
f"https://nekos.life/api/v2/img/{ctx.invoked_with}"
) as response:
if response.status == 200:
data = await response.json()
else:
# Send an error embed.
embed.title = "API error"
embed.description = ("Sorry, a problem has occurred when "
"trying to interact with the "
"nekos.life API")
embed.description = (
"Sorry, a problem has occurred when trying to interact "
"with the nekos.life API"
)
return await ctx.send(embed=embed)

# Place the nekos.life's gif in the embed.
embed.set_image(url=data["url"])

# Make a custom message based on the command's template message.
if ctx.author.id != member.id:
embed.description = actions[ctx.invoked_with].format(author=ctx.author.name,
target=member.name)
embed.description = actions[ctx.invoked_with].format(
author=ctx.author.name, target=member.name
)
else:
embed.description = ("Aww, I see you are lonely, I will "
f"{ctx.invoked_with} you")
embed.description = (
f"Aww, I see you are lonely, I will {ctx.invoked_with} you"
)

await ctx.send(embed=embed)

Expand All @@ -88,9 +90,11 @@ async def apod(self, ctx):
apod = await response.text()
else:
# Send an error embed.
embed.description = ("Sorry, a problem has occurred when "
"trying to interact with the apod "
"website")
embed.description = (
"Sorry, a problem has occurred when "
"trying to interact with the apod "
"website"
)
return await ctx.send(embed=embed)

# Collect informations.
Expand Down Expand Up @@ -121,10 +125,13 @@ def __init__(self, bot, config):

self.confession_queue = {}

self.confession_is_confirm_e: Optional[bool] = None
self.confession_msg: Optional[str] = None

@commands.Cog.listener()
@user_only()
async def on_message(self, message):
""""Decide to store message and send a confirm message."""
""" "Decide to store message and send a confirm message."""
self.confession_is_confirm_e = False

# Check if Confession is enabled.
Expand All @@ -136,24 +143,27 @@ async def on_message(self, message):
if not message.guild:
self.confession_is_confirm_e = True
self.confession_msg = message
await message.channel.send("React to this message to send it to "
"the confession channel")
await message.channel.send(
"React to this message to send it to the confession channel"
)

@commands.Cog.listener()
async def on_raw_reaction_add(self, reaction):
"""Decide to send stored confession message."""
reaction_user = await self.bot.fetch_user(reaction.user_id)
reaction_channel = await self.bot.fetch_channel(reaction.channel_id)
confession_channel = await self.bot.fetch_channel(self.config["CONFESSION"]["CHANNEL"])
user_in_chan_guild = await confession_channel.guild.fetch_member(reaction.user_id)

if (reaction_user.bot
or not self.confession_is_confirm_e):
confession_channel = await self.bot.fetch_channel(
self.config["CONFESSION"]["CHANNEL"]
)
user_in_chan_guild = await confession_channel.guild.fetch_member(
reaction.user_id
)

if reaction_user.bot or not self.confession_is_confirm_e:
return

# If reaction is in DM and user is present in confession channel,
# send the message.
if (reaction_channel.type.name == "private"
and user_in_chan_guild is not None):
# send the message.
if reaction_channel.type.name == "private" and user_in_chan_guild is not None:
self.confession_is_confirm_e = False
await confession_channel.send(self.confession_msg.content)
11 changes: 5 additions & 6 deletions cogs/moderation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""Contains cog related to moderation."""

import re
import unicodedata

import discord
from discord.ext import commands

from src.utils import user_only
Expand All @@ -16,14 +14,15 @@ def __init__(self, bot, config):
self.bot = bot
self.config = config

self.regex_patterns = {chan_id: re.compile(pattern)
for chan_id, pattern in config["REGEX_CHANNELS"].items()}
self.regex_patterns = {
chan_id: re.compile(pattern)
for chan_id, pattern in config["REGEX_CHANNELS"].items()
}

@commands.Cog.listener()
@user_only()
async def on_message(self, message):
"""Delete message that does not match the channel regex."""
pattern = self.regex_patterns.get(str(message.channel.id))
if pattern:
if pattern := self.regex_patterns.get(str(message.channel.id)):
if not pattern.fullmatch(message.content):
await message.delete()
Loading