Skip to content

Commit

Permalink
Merge branch 'develop' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
raidensakura committed Mar 31, 2024
2 parents 9770549 + 3d6616a commit 3bf3f9d
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 56 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ temp/
test.py

# Unused repository files
**/.git/
.env.example
.gitignore
.dockerignore
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/load-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ jobs:
with:
path: ~/.cache/pypoetry/virtualenvs
key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }}

- name: Maybe install dependencies
if: steps.cache-venv.outputs.cache-hit != 'true'
run: poetry install --no-root

- name: Run script loadcheck.py
run: |
poetry run python .github/workflows/scripts/loadcheck.py
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ however, insignificant breaking changes do not guarantee a major version bump, s

# [UNRELEASED]

### Added
- Add `?avatar` command for changing the bot avatar with a given URL or attachment.

### Fixed
- Improve select options in the select menu (Thanks #3298).
- Fix the bot failing catastrophically when plugin registry fails to load (Thanks #3328).

# v4.2.0

### Breaking
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Check out the [contributing guidelines](https://github.com/raidensakura/modmail/

The [develop](https://github.com/raidensakura/modmail/tree/develop) branch is where most of the features are tested before stable release.

This project has included pre-commit script that automatically runs black and ruff linter on every commit.
This project has included pre-commit script that automatically run black and ruff linter on every commit.

1. Install development dependencies
```console
Expand Down
14 changes: 12 additions & 2 deletions cogs/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ async def cog_load(self):

async def populate_registry(self):
url = "https://raw.githubusercontent.com/modmail-dev/modmail/master/plugins/registry.json"
async with self.bot.session.get(url) as resp:
self.registry = json.loads(await resp.text())
try:
async with self.bot.session.get(url) as resp:
self.registry = json.loads(await resp.text())
except asyncio.TimeoutError:
logger.warning("Failed to fetch registry. Loading with empty registry")

async def initial_load_plugins(self):
for plugin_name in list(self.bot.config["plugins"]):
Expand Down Expand Up @@ -622,6 +625,13 @@ async def plugins_registry(self, ctx, *, plugin_name: typing.Union[int, str] = N

registry = sorted(self.registry.items(), key=lambda elem: elem[0])

if not registry:
embed = discord.Embed(
color=self.bot.error_color,
description="Registry is empty. This could be because it failed to load.",
)
return await ctx.send(embed=embed)

if isinstance(plugin_name, int):
index = plugin_name - 1
if index < 0:
Expand Down
66 changes: 58 additions & 8 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from textwrap import indent
from typing import Union

import aiohttp
import discord
from aiohttp import ClientResponseError
from discord.enums import ActivityType, Status
Expand All @@ -25,7 +26,7 @@
from core.changelog import Changelog
from core.models import HostingMethod, InvalidConfigError, PermissionLevel, UnseenFormatter, getLogger
from core.paginator import EmbedPaginatorSession, MessagePaginatorSession
from core.utils import DummyParam, trigger_typing, truncate
from core.utils import DummyParam, is_image_url, trigger_typing, truncate

logger = getLogger(__name__)

Expand Down Expand Up @@ -276,7 +277,7 @@ def cog_unload(self):

@commands.command()
@checks.has_permissions(PermissionLevel.REGULAR)
@utils.trigger_typing
@trigger_typing
async def changelog(self, ctx, version: str.lower = ""):
"""Shows the changelog of the Modmail."""
changelog = await Changelog.from_url(self.bot)
Expand Down Expand Up @@ -309,7 +310,7 @@ async def changelog(self, ctx, version: str.lower = ""):

@commands.command(aliases=["info"])
@checks.has_permissions(PermissionLevel.REGULAR)
@utils.trigger_typing
@trigger_typing
async def about(self, ctx):
"""Shows information about this bot."""
embed = discord.Embed(color=self.bot.main_color, timestamp=discord.utils.utcnow())
Expand Down Expand Up @@ -385,7 +386,7 @@ async def about(self, ctx):

@commands.command(aliases=["sponsor"])
@checks.has_permissions(PermissionLevel.REGULAR)
@utils.trigger_typing
@trigger_typing
async def sponsors(self, ctx):
"""Shows the sponsors of this project."""

Expand All @@ -407,7 +408,7 @@ async def sponsors(self, ctx):

@commands.group(invoke_without_command=True)
@checks.has_permissions(PermissionLevel.OWNER)
@utils.trigger_typing
@trigger_typing
async def debug(self, ctx):
"""Shows the recent application logs of the bot."""

Expand Down Expand Up @@ -460,7 +461,7 @@ async def debug(self, ctx):

@debug.command(name="hastebin", aliases=["haste"])
@checks.has_permissions(PermissionLevel.OWNER)
@utils.trigger_typing
@trigger_typing
async def debug_hastebin(self, ctx):
"""Posts application-logs to Hastebin."""

Expand Down Expand Up @@ -497,7 +498,7 @@ async def debug_hastebin(self, ctx):

@debug.command(name="clear", aliases=["wipe"])
@checks.has_permissions(PermissionLevel.OWNER)
@utils.trigger_typing
@trigger_typing
async def debug_clear(self, ctx):
"""Clears the locally cached logs."""

Expand Down Expand Up @@ -674,7 +675,7 @@ async def before_loop_presence(self):

@commands.command()
@checks.has_permissions(PermissionLevel.ADMINISTRATOR)
@utils.trigger_typing
@trigger_typing
async def ping(self, ctx):
"""Pong! Returns your websocket latency."""
embed = discord.Embed(
Expand Down Expand Up @@ -2191,6 +2192,55 @@ def paginate(text: str):

await self.bot.add_reaction(ctx.message, "\u2705")

@commands.command(name="avatar")
@commands.cooldown(3, 10, commands.BucketType.default)
@checks.has_permissions(PermissionLevel.OWNER)
@trigger_typing
async def avatar(self, ctx: commands.Context, url: Union[str, None]):
"""
Updates the bot's avatar within discord.
"""
if not ctx.message.attachments and (url is None or not is_image_url(url)):
embed = discord.Embed(
title="Error",
description="You need to upload or link a valid image file.",
color=self.bot.error_color,
)
return await ctx.send(embed=embed)
dc_avatar = None
if ctx.message.attachments:
dc_avatar = await ctx.message.attachments[0].read()
elif url:
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
dc_avatar = await resp.read()

embed = None
if dc_avatar:
try:
await self.bot.user.edit(avatar=dc_avatar)
logger.info("Bot Avatar updated.")
embed = discord.Embed(
title="Successfully updated",
description="Successfully updated avatar.",
color=self.bot.main_color,
)
except Exception as e:
logger.error(f"Uploading the avatar to discord failed: {e}")
embed = discord.Embed(
title="Error",
description=f"Could not upload avatar to Discord: {e}.",
color=self.bot.error_color,
)
await ctx.send(embed=embed)
else:
embed = discord.Embed(
title="Error",
description="Could not fetch an image file from given URL.",
color=self.bot.error_color,
)
await ctx.send(embed=embed)


async def setup(bot):
await bot.add_cog(Utility(bot))
Loading

0 comments on commit 3bf3f9d

Please sign in to comment.