Created
August 6, 2023 01:24
-
-
Save genbtc/4ada61b06046ed6a1ca593613bc3b479 to your computer and use it in GitHub Desktop.
cled.py
This file contains hidden or 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
#!/usr/bin/env python3 | |
import aioconsole | |
import colorama | |
import configparser | |
import json | |
import discord | |
from discord.ext import commands | |
import atexit, os, sys, readline | |
histfile = os.path.join(os.path.expanduser("~"), ".discord_history") | |
try: | |
readline.read_history_file(histfile) | |
# default history len is -1 (infinite) | |
readline.set_history_length(1000000) | |
except FileNotFoundError: | |
pass | |
atexit.register(readline.write_history_file, histfile) | |
# Custom Client Class | |
class MyClient(discord.Client): | |
def read_config(self): | |
# read config on startup | |
try: | |
with open("config.json") as f: | |
config = json.load(f) | |
except FileNotFoundError: | |
print("Please fill the config.json file with a token dict.") | |
exit(1) | |
# set config and token | |
self.config = config | |
self.token = config["token"] | |
# Bot Ready | |
async def on_ready(self): | |
print(f'Logged in as {self.user} (ID: {self.user.id})') | |
print('--------------------------------------------------') | |
# self.loop.create_task(self.main_loop()) | |
# main loop, running | |
async def main_loop(self): | |
print("main loop Ready!") | |
self.initialized = True | |
while (True): | |
input_line = sys.stdin.readline() | |
if input_line is None: | |
print("no input! exiting input loop") | |
break | |
if (input_line.startswith('!exit')): | |
exit(1) | |
if (input_line.startswith('/hello')): | |
print(f'Still logged in as {self.user} (ID: {self.user.id})') | |
print("leaving main input loop") | |
# pretty print output | |
async def format_message(self, message): | |
return (color(self, f"/{message.guild.name}", "server") + | |
color(self, f"/{message.channel.name}", "channel") + | |
color(self, f"/{encode_id(message.id)} [{message.author.name}] {message.content}", "text")) | |
# Message Received | |
async def on_message(self, message): | |
# don't respond to ourselves | |
if message.author == self.user: | |
return | |
if message.content.startswith('!hello'): | |
await message.reply('Hello!', mention_author=True) | |
# ping/pong test | |
if message.content == 'ping': | |
await message.channel.send('pong') | |
# pretty print discord channel messages | |
print(self.format_message(self, message)) | |
# New user join guild | |
async def on_member_join(self, member): | |
guild = member.guild | |
if guild.system_channel is not None: | |
to_send = f'Welcome {member.mention} to {guild.name}!' | |
await guild.system_channel.send(to_send) | |
# client bot class init | |
client = MyClient() | |
# config parser | |
client.read_config() | |
# colorama init | |
colorama.init() | |
# setup debug verbose logging | |
import logging | |
import logging.handlers | |
logger = logging.getLogger('discord') | |
logger.setLevel(logging.DEBUG) | |
logging.getLogger('discord.http').setLevel(logging.INFO) | |
handler = logging.handlers.RotatingFileHandler( | |
filename='discord.log', | |
encoding='utf-8', | |
maxBytes=32 * 1024 * 1024, # 32 MiB | |
backupCount=5, # Rotate through 5 files | |
) | |
dt_fmt = '%Y-%m-%d %H:%M:%S' | |
formatter = logging.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', dt_fmt, style='{') | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
# start bot (last call) | |
#client.run(client.token, bot=False) | |
print("this bot works. disabled though") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment