mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-12-08 10:22:31 -05:00
[Core] Subclassing of Bot class, Python version check
`send_cmd_help` and `user_allowed` are now part of Bot
This commit is contained in:
@@ -621,9 +621,10 @@ class Owner:
|
|||||||
@commands.command()
|
@commands.command()
|
||||||
async def uptime(self):
|
async def uptime(self):
|
||||||
"""Shows Red's uptime"""
|
"""Shows Red's uptime"""
|
||||||
up = abs(self.bot.uptime - int(time.perf_counter()))
|
now = datetime.datetime.now()
|
||||||
up = str(datetime.timedelta(seconds=up))
|
uptime = (now - self.bot.uptime).seconds
|
||||||
await self.bot.say("`Uptime: {}`".format(up))
|
uptime = datetime.timedelta(seconds=uptime)
|
||||||
|
await self.bot.say("`Uptime: {}`".format(uptime))
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def version(self):
|
async def version(self):
|
||||||
|
|||||||
134
red.py
134
red.py
@@ -6,8 +6,10 @@ import logging
|
|||||||
import logging.handlers
|
import logging.handlers
|
||||||
import shutil
|
import shutil
|
||||||
import traceback
|
import traceback
|
||||||
|
import datetime
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
assert sys.version_info >= (3, 5)
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
import discord
|
import discord
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -16,10 +18,17 @@ except ImportError:
|
|||||||
"and do ALL the steps in order.\n"
|
"and do ALL the steps in order.\n"
|
||||||
"https://twentysix26.github.io/Red-Docs/\n")
|
"https://twentysix26.github.io/Red-Docs/\n")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
except AssertionError:
|
||||||
|
print("Red needs Python 3.5 or superior.\n"
|
||||||
|
"Consult the guide for your operating system "
|
||||||
|
"and do ALL the steps in order.\n"
|
||||||
|
"https://twentysix26.github.io/Red-Docs/\n")
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
from cogs.utils.settings import Settings
|
from cogs.utils.settings import Settings
|
||||||
from cogs.utils.dataIO import dataIO
|
from cogs.utils.dataIO import dataIO
|
||||||
from cogs.utils.chat_formatting import inline
|
from cogs.utils.chat_formatting import inline
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
#
|
#
|
||||||
# Red, a Discord bot by Twentysix, based on discord.py and its command extension
|
# Red, a Discord bot by Twentysix, based on discord.py and its command extension
|
||||||
@@ -33,6 +42,62 @@ from cogs.utils.chat_formatting import inline
|
|||||||
description = "Red - A multifunction Discord bot by Twentysix"
|
description = "Red - A multifunction Discord bot by Twentysix"
|
||||||
|
|
||||||
|
|
||||||
|
class Bot(commands.Bot):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.counter = Counter()
|
||||||
|
self.uptime = datetime.datetime.now()
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
async def send_cmd_help(self, ctx):
|
||||||
|
if ctx.invoked_subcommand:
|
||||||
|
pages = bot.formatter.format_help_for(ctx, ctx.invoked_subcommand)
|
||||||
|
for page in pages:
|
||||||
|
await bot.send_message(ctx.message.channel, page)
|
||||||
|
else:
|
||||||
|
pages = bot.formatter.format_help_for(ctx, ctx.command)
|
||||||
|
for page in pages:
|
||||||
|
await bot.send_message(ctx.message.channel, page)
|
||||||
|
|
||||||
|
def user_allowed(self, message):
|
||||||
|
author = message.author
|
||||||
|
|
||||||
|
if author.bot or author == self.user:
|
||||||
|
return False
|
||||||
|
|
||||||
|
mod = self.get_cog('Mod')
|
||||||
|
|
||||||
|
if mod is not None:
|
||||||
|
if settings.owner == author.id:
|
||||||
|
return True
|
||||||
|
if not message.channel.is_private:
|
||||||
|
server = message.server
|
||||||
|
names = (settings.get_server_admin(
|
||||||
|
server), settings.get_server_mod(server))
|
||||||
|
results = map(
|
||||||
|
lambda name: discord.utils.get(author.roles, name=name),
|
||||||
|
names)
|
||||||
|
for r in results:
|
||||||
|
if r is not None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if author.id in mod.blacklist_list:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if mod.whitelist_list:
|
||||||
|
if author.id not in mod.whitelist_list:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if not message.channel.is_private:
|
||||||
|
if message.server.id in mod.ignore_list["SERVERS"]:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if message.channel.id in mod.ignore_list["CHANNELS"]:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class Formatter(commands.HelpFormatter):
|
class Formatter(commands.HelpFormatter):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
@@ -51,9 +116,12 @@ class Formatter(commands.HelpFormatter):
|
|||||||
|
|
||||||
formatter = Formatter(show_check_failure=False)
|
formatter = Formatter(show_check_failure=False)
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix=["_"], formatter=formatter,
|
bot = Bot(command_prefix=["_"], formatter=formatter,
|
||||||
description=description, pm_help=None)
|
description=description, pm_help=None)
|
||||||
|
|
||||||
|
send_cmd_help = bot.send_cmd_help # Backwards
|
||||||
|
user_allowed = bot.user_allowed # compatibility
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
|
||||||
|
|
||||||
@@ -64,8 +132,6 @@ async def on_ready():
|
|||||||
users = len(set(bot.get_all_members()))
|
users = len(set(bot.get_all_members()))
|
||||||
servers = len(bot.servers)
|
servers = len(bot.servers)
|
||||||
channels = len([c for c in bot.get_all_channels()])
|
channels = len([c for c in bot.get_all_channels()])
|
||||||
if not hasattr(bot, "uptime"):
|
|
||||||
bot.uptime = int(time.perf_counter())
|
|
||||||
if settings.login_type == "token" and settings.owner == "id_here":
|
if settings.login_type == "token" and settings.owner == "id_here":
|
||||||
await set_bot_owner()
|
await set_bot_owner()
|
||||||
print('------')
|
print('------')
|
||||||
@@ -91,11 +157,12 @@ async def on_ready():
|
|||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_command(command, ctx):
|
async def on_command(command, ctx):
|
||||||
pass
|
bot.counter["processed_commands"] += 1
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_message(message):
|
async def on_message(message):
|
||||||
|
bot.counter["messages_read"] += 1
|
||||||
if user_allowed(message):
|
if user_allowed(message):
|
||||||
await bot.process_commands(message)
|
await bot.process_commands(message)
|
||||||
|
|
||||||
@@ -126,57 +193,6 @@ async def on_command_error(error, ctx):
|
|||||||
else:
|
else:
|
||||||
logger.exception(type(error).__name__, exc_info=error)
|
logger.exception(type(error).__name__, exc_info=error)
|
||||||
|
|
||||||
async def send_cmd_help(ctx):
|
|
||||||
if ctx.invoked_subcommand:
|
|
||||||
pages = bot.formatter.format_help_for(ctx, ctx.invoked_subcommand)
|
|
||||||
for page in pages:
|
|
||||||
await bot.send_message(ctx.message.channel, page)
|
|
||||||
else:
|
|
||||||
pages = bot.formatter.format_help_for(ctx, ctx.command)
|
|
||||||
for page in pages:
|
|
||||||
await bot.send_message(ctx.message.channel, page)
|
|
||||||
|
|
||||||
|
|
||||||
def user_allowed(message):
|
|
||||||
|
|
||||||
author = message.author
|
|
||||||
|
|
||||||
if author.bot or author == bot.user:
|
|
||||||
return False
|
|
||||||
|
|
||||||
mod = bot.get_cog('Mod')
|
|
||||||
|
|
||||||
if mod is not None:
|
|
||||||
if settings.owner == author.id:
|
|
||||||
return True
|
|
||||||
if not message.channel.is_private:
|
|
||||||
server = message.server
|
|
||||||
names = (settings.get_server_admin(
|
|
||||||
server), settings.get_server_mod(server))
|
|
||||||
results = map(
|
|
||||||
lambda name: discord.utils.get(author.roles, name=name), names)
|
|
||||||
for r in results:
|
|
||||||
if r is not None:
|
|
||||||
return True
|
|
||||||
|
|
||||||
if author.id in mod.blacklist_list:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if mod.whitelist_list:
|
|
||||||
if author.id not in mod.whitelist_list:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if not message.channel.is_private:
|
|
||||||
if message.server.id in mod.ignore_list["SERVERS"]:
|
|
||||||
return False
|
|
||||||
|
|
||||||
if message.channel.id in mod.ignore_list["CHANNELS"]:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
async def get_oauth_url():
|
async def get_oauth_url():
|
||||||
try:
|
try:
|
||||||
data = await bot.application_info()
|
data = await bot.application_info()
|
||||||
@@ -331,13 +347,7 @@ def set_cog(cog, value):
|
|||||||
dataIO.save_json("data/red/cogs.json", data)
|
dataIO.save_json("data/red/cogs.json", data)
|
||||||
|
|
||||||
def load_cogs():
|
def load_cogs():
|
||||||
try:
|
no_prompt = "--no-prompt" in sys.argv[1:]
|
||||||
if sys.argv[1] == "--no-prompt":
|
|
||||||
no_prompt = True
|
|
||||||
else:
|
|
||||||
no_prompt = False
|
|
||||||
except:
|
|
||||||
no_prompt = False
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
registry = dataIO.load_json("data/red/cogs.json")
|
registry = dataIO.load_json("data/red/cogs.json")
|
||||||
|
|||||||
Reference in New Issue
Block a user