mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[Core] Use menus for [p]servers, improve [p]leave (#4831)
* [Core] Use menus for [p]servers, improve [p]leave * Apply suggestions from code review Co-authored-by: Samuel <50765275+npc203@users.noreply.github.com> * Few more changes * Add empty line... * style Co-authored-by: jack1142 <6032823+jack1142@users.noreply.github.com> Co-authored-by: Samuel <50765275+npc203@users.noreply.github.com>
This commit is contained in:
parent
adda30cbee
commit
c25095ba2d
@ -16,6 +16,8 @@ import getpass
|
|||||||
import pip
|
import pip
|
||||||
import traceback
|
import traceback
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from redbot.core.utils.menus import menu, DEFAULT_CONTROLS
|
||||||
|
from redbot.core.commands import GuildConverter
|
||||||
from string import ascii_letters, digits
|
from string import ascii_letters, digits
|
||||||
from typing import TYPE_CHECKING, Union, Tuple, List, Optional, Iterable, Sequence, Dict, Set
|
from typing import TYPE_CHECKING, Union, Tuple, List, Optional, Iterable, Sequence, Dict, Set
|
||||||
|
|
||||||
@ -1418,70 +1420,76 @@ class Core(commands.commands._RuleDropper, commands.Cog, CoreLogic):
|
|||||||
await ctx.send("The new permissions level has been set.")
|
await ctx.send("The new permissions level has been set.")
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@commands.guild_only()
|
|
||||||
@checks.is_owner()
|
@checks.is_owner()
|
||||||
async def leave(self, ctx: commands.Context):
|
async def leave(self, ctx: commands.Context, *servers: GuildConverter):
|
||||||
"""Leaves the current server."""
|
"""Leaves servers.
|
||||||
await ctx.send(_("Are you sure you want me to leave this server? (y/n)"))
|
|
||||||
|
|
||||||
|
If no server IDs are passed the local server will be left instead."""
|
||||||
|
guilds = servers
|
||||||
|
if ctx.guild is None and not guilds:
|
||||||
|
return await ctx.send(_("You need to specify at least one server ID."))
|
||||||
|
|
||||||
|
leaving_local_guild = not guilds
|
||||||
|
|
||||||
|
if leaving_local_guild:
|
||||||
|
guilds = (ctx.guild,)
|
||||||
|
msg = (
|
||||||
|
_("You haven't passed any server ID. Do you want me to leave this server?")
|
||||||
|
+ " (y/n)"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
msg = (
|
||||||
|
_("Are you sure you want me to leave these servers?")
|
||||||
|
+ " (y/n):\n"
|
||||||
|
+ "\n".join(f"- {guild.name} (`{guild.id}`)" for guild in guilds)
|
||||||
|
)
|
||||||
|
|
||||||
|
for guild in guilds:
|
||||||
|
if guild.owner.id == ctx.me.id:
|
||||||
|
return await ctx.send(
|
||||||
|
_("I cannot leave the server `{server_name}`: I am the owner of it.").format(
|
||||||
|
server_name=guild.name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
for page in pagify(msg):
|
||||||
|
await ctx.send(page)
|
||||||
pred = MessagePredicate.yes_or_no(ctx)
|
pred = MessagePredicate.yes_or_no(ctx)
|
||||||
try:
|
try:
|
||||||
await self.bot.wait_for("message", check=pred)
|
await self.bot.wait_for("message", check=pred, timeout=30)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
await ctx.send(_("Response timed out."))
|
await ctx.send(_("Response timed out."))
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
if pred.result is True:
|
if pred.result is True:
|
||||||
await ctx.send(_("Alright. Bye :wave:"))
|
if leaving_local_guild is True:
|
||||||
log.debug(_("Leaving guild '{}'").format(ctx.guild.name))
|
await ctx.send(_("Alright. Bye :wave:"))
|
||||||
await ctx.guild.leave()
|
else:
|
||||||
|
await ctx.send(
|
||||||
|
_("Alright. Leaving {number} servers...").format(number=len(guilds))
|
||||||
|
)
|
||||||
|
for guild in guilds:
|
||||||
|
log.debug("Leaving guild '%s' (%s)", guild.name, guild.id)
|
||||||
|
await guild.leave()
|
||||||
else:
|
else:
|
||||||
await ctx.send(_("Alright, I'll stay then. :)"))
|
if leaving_local_guild is True:
|
||||||
|
await ctx.send(_("Alright, I'll stay then. :)"))
|
||||||
|
else:
|
||||||
|
await ctx.send(_("Alright, I'm not leaving those servers."))
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@checks.is_owner()
|
@checks.is_owner()
|
||||||
async def servers(self, ctx: commands.Context):
|
async def servers(self, ctx: commands.Context):
|
||||||
"""Lists and allows [botname] to leave servers."""
|
"""Lists the servers [botname] is currently in."""
|
||||||
guilds = sorted(list(self.bot.guilds), key=lambda s: s.name.lower())
|
guilds = sorted(self.bot.guilds, key=lambda s: s.name.lower())
|
||||||
msg = ""
|
msg = "\n".join(f"{guild.name} (`{guild.id}`)\n" for guild in guilds)
|
||||||
responses = []
|
|
||||||
for i, server in enumerate(guilds, 1):
|
|
||||||
msg += "{}: {} (`{}`)\n".format(i, server.name, server.id)
|
|
||||||
responses.append(str(i))
|
|
||||||
|
|
||||||
for page in pagify(msg, ["\n"]):
|
pages = list(pagify(msg, ["\n"], page_length=1000))
|
||||||
await ctx.send(page)
|
|
||||||
|
|
||||||
query = await ctx.send(_("To leave a server, just type its number."))
|
if len(pages) == 1:
|
||||||
|
await ctx.send(pages[0])
|
||||||
pred = MessagePredicate.contained_in(responses, ctx)
|
|
||||||
try:
|
|
||||||
await self.bot.wait_for("message", check=pred, timeout=15)
|
|
||||||
except asyncio.TimeoutError:
|
|
||||||
try:
|
|
||||||
await query.delete()
|
|
||||||
except discord.errors.NotFound:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
await self.leave_confirmation(guilds[pred.result], ctx)
|
await menu(ctx, pages, DEFAULT_CONTROLS)
|
||||||
|
|
||||||
async def leave_confirmation(self, guild, ctx):
|
|
||||||
if guild.owner.id == ctx.bot.user.id:
|
|
||||||
await ctx.send(_("I cannot leave a guild I am the owner of."))
|
|
||||||
return
|
|
||||||
|
|
||||||
await ctx.send(_("Are you sure you want me to leave {}? (yes/no)").format(guild.name))
|
|
||||||
pred = MessagePredicate.yes_or_no(ctx)
|
|
||||||
try:
|
|
||||||
await self.bot.wait_for("message", check=pred, timeout=15)
|
|
||||||
if pred.result is True:
|
|
||||||
await guild.leave()
|
|
||||||
if guild != ctx.guild:
|
|
||||||
await ctx.send(_("Done."))
|
|
||||||
else:
|
|
||||||
await ctx.send(_("Alright then."))
|
|
||||||
except asyncio.TimeoutError:
|
|
||||||
await ctx.send(_("Response timed out."))
|
|
||||||
|
|
||||||
@commands.command(require_var_positional=True)
|
@commands.command(require_var_positional=True)
|
||||||
@checks.is_owner()
|
@checks.is_owner()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user