Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
[logbot] bringing back old tests
Browse files
app.py
CHANGED
|
@@ -1,74 +1,197 @@
|
|
| 1 |
import discord
|
| 2 |
import os
|
| 3 |
import threading
|
| 4 |
-
|
|
|
|
| 5 |
import json
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
import gradio_client
|
| 9 |
-
import gradio as gr
|
| 10 |
from gradio_client import Client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 13 |
-
intents = discord.Intents.
|
|
|
|
|
|
|
| 14 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
@bot.event
|
| 21 |
-
async def
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# on_delete
|
| 26 |
@bot.event
|
| 27 |
async def on_message_delete(message):
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
@bot.event
|
| 41 |
-
async def
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
author_id = str(message.author.id) # dictionary pairs (ID -> TOTAL XP)
|
| 46 |
-
xp_data.setdefault(author_id, 0) # default if it doesn't already exist
|
| 47 |
-
|
| 48 |
-
xp_data[author_id] += XP_PER_MESSAGE
|
| 49 |
-
print(f"xp_data: {xp_data}")
|
| 50 |
-
save_xp_data()
|
| 51 |
-
|
| 52 |
-
user = bot.get_user(811235357663297546)
|
| 53 |
-
try:
|
| 54 |
-
await user.send(f"xp_data: {xp_data}")
|
| 55 |
-
except discord.HTTPException:
|
| 56 |
-
await ctx.send("Failed to send a DM.")
|
| 57 |
-
|
| 58 |
-
await bot.process_commands(message)
|
| 59 |
-
except Exception as e:
|
| 60 |
-
print(f"Error: {e}")
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
|
| 66 |
-
|
| 67 |
-
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 68 |
def run_bot():
|
| 69 |
bot.run(DISCORD_TOKEN)
|
|
|
|
| 70 |
threading.Thread(target=run_bot).start()
|
|
|
|
| 71 |
def greet(name):
|
| 72 |
return "Hello " + name + "!"
|
|
|
|
| 73 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
import discord
|
| 2 |
import os
|
| 3 |
import threading
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
import json
|
| 7 |
+
import random
|
| 8 |
+
import time
|
| 9 |
+
import re
|
| 10 |
+
from discord import Embed, Color
|
| 11 |
+
from discord.ext import commands
|
| 12 |
|
|
|
|
|
|
|
| 13 |
from gradio_client import Client
|
| 14 |
+
from PIL import Image
|
| 15 |
+
from ratelimiter import RateLimiter
|
| 16 |
+
|
| 17 |
+
from datetime import datetime # for times
|
| 18 |
+
from pytz import timezone # for times
|
| 19 |
+
|
| 20 |
+
import asyncio # check if used
|
| 21 |
+
|
| 22 |
+
zurich_tz = timezone("Europe/Zurich")
|
| 23 |
+
|
| 24 |
+
def convert_to_timezone(dt, tz):
|
| 25 |
+
return dt.astimezone(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
|
| 26 |
|
| 27 |
DISCORD_TOKEN = os.environ.get("DISCORD_TOKEN", None)
|
| 28 |
+
intents = discord.Intents.default()
|
| 29 |
+
intents.message_content = True
|
| 30 |
+
|
| 31 |
bot = commands.Bot(command_prefix='!', intents=intents)
|
| 32 |
|
| 33 |
+
LOG_CHANNEL_ID =
|
| 34 |
+
|
| 35 |
+
#rate_limiter = RateLimiter(max_calls=10, period=60) # needs testing
|
| 36 |
+
|
| 37 |
+
# todo
|
| 38 |
+
'''
|
| 39 |
+
Make sure only can post in #bot-test
|
| 40 |
+
|
| 41 |
+
discord.gg / discord/invite (block completely, need to be cautious)
|
| 42 |
+
attempted @everyone / @here pings, or trying to use those strings (ping @alerts)
|
| 43 |
+
ping alerts for list of keywords
|
| 44 |
+
|
| 45 |
|
| 46 |
+
|
| 47 |
+
'''
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# moderation stuff----------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 53 |
|
| 54 |
@bot.event
|
| 55 |
+
async def on_message_edit(before, after):
|
| 56 |
+
if before.author == bot.user:
|
| 57 |
+
return
|
| 58 |
+
|
| 59 |
+
if before.content != after.content:
|
| 60 |
+
embed = Embed(color=Color.orange())
|
| 61 |
+
embed.set_author(name=f"{before.author} ID: {before.author.id}", icon_url=before.author.avatar.url)
|
| 62 |
+
embed.title = "Message Edited"
|
| 63 |
+
embed.description = f"**Before:** {before.content or '*(empty message)*'}\n**After:** {after.content or '*(empty message)*'}"
|
| 64 |
+
embed.add_field(name="Author Username", value=before.author.name, inline=True)
|
| 65 |
+
embed.add_field(name="Channel", value=before.channel.mention, inline=True)
|
| 66 |
+
#embed.add_field(name="Message Created On", value=before.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
|
| 67 |
+
embed.add_field(name="Message Created On", value=convert_to_timezone(before.created_at, zurich_tz), inline=True)
|
| 68 |
+
embed.add_field(name="Message ID", value=before.id, inline=True)
|
| 69 |
+
embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{before.guild.id}/{before.channel.id}/{before.id})", inline=True)
|
| 70 |
|
| 71 |
+
if before.attachments:
|
| 72 |
+
attachment_urls = "\n".join([attachment.url for attachment in before.attachments])
|
| 73 |
+
embed.add_field(name="Attachments", value=attachment_urls, inline=False)
|
| 74 |
+
|
| 75 |
+
#embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
|
| 76 |
+
embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
|
| 77 |
+
|
| 78 |
+
await bot.log_channel.send(embed=embed)
|
| 79 |
|
|
|
|
| 80 |
@bot.event
|
| 81 |
async def on_message_delete(message):
|
| 82 |
+
if message.author == bot.user:
|
| 83 |
+
return
|
| 84 |
|
| 85 |
+
embed = Embed(color=Color.red())
|
| 86 |
+
embed.set_author(name=f"{message.author} ID: {message.author.id}", icon_url=message.author.avatar.url)
|
| 87 |
+
embed.title = "Message Deleted"
|
| 88 |
+
embed.description = message.content or "*(empty message)*"
|
| 89 |
+
embed.add_field(name="Author Username", value=message.author.name, inline=True)
|
| 90 |
+
embed.add_field(name="Channel", value=message.channel.mention, inline=True)
|
| 91 |
+
#embed.add_field(name="Message Created On", value=message.created_at.strftime("%Y-%m-%d %H:%M:%S UTC"), inline=True)
|
| 92 |
+
embed.add_field(name="Message Created On", value=convert_to_timezone(message.created_at, zurich_tz), inline=True)
|
| 93 |
+
embed.add_field(name="Message ID", value=message.id, inline=True)
|
| 94 |
+
embed.add_field(name="Message Jump URL", value=f"[Jump to message!](https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id})", inline=True)
|
| 95 |
|
| 96 |
+
if message.attachments:
|
| 97 |
+
attachment_urls = "\n".join([attachment.url for attachment in message.attachments])
|
| 98 |
+
embed.add_field(name="Attachments", value=attachment_urls, inline=False)
|
| 99 |
|
| 100 |
+
#embed.set_footer(text=f"{datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}")
|
| 101 |
+
embed.set_footer(text=f"{convert_to_timezone(datetime.utcnow(), zurich_tz)}")
|
| 102 |
|
| 103 |
+
await bot.log_channel.send(embed=embed)
|
| 104 |
+
|
| 105 |
+
# nickname stuff
|
| 106 |
@bot.event
|
| 107 |
+
async def on_member_update(before, after):
|
| 108 |
+
if before.nick != after.nick:
|
| 109 |
+
embed = Embed(description=f'{before} changed their nickname to {after.nick}', color=Color.blue())
|
| 110 |
+
await bot.log_channel.send(embed=embed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
@bot.event
|
| 113 |
+
async def on_member_ban(guild, user):
|
| 114 |
+
# member banned from the guild
|
| 115 |
+
embed = Embed(description=f'Member {user.mention} was banned from the guild', color=Color.red())
|
| 116 |
+
await bot.log_channel.send(embed=embed)
|
| 117 |
|
| 118 |
+
@bot.event
|
| 119 |
+
async def on_member_unban(guild, user):
|
| 120 |
+
# member unbanned from the guild
|
| 121 |
+
embed = Embed(description=f'Member {user.mention} was unbanned from the guild', color=Color.green())
|
| 122 |
+
await bot.log_channel.send(embed=embed)
|
| 123 |
+
|
| 124 |
+
# admin stuff---------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 125 |
+
|
| 126 |
+
@bot.event
|
| 127 |
+
async def on_guild_channel_create(channel):
|
| 128 |
+
# creating channels
|
| 129 |
+
embed = Embed(description=f'Channel {channel.mention} was created', color=Color.green())
|
| 130 |
+
await bot.log_channel.send(embed=embed)
|
| 131 |
+
|
| 132 |
+
@bot.event
|
| 133 |
+
async def on_guild_channel_delete(channel):
|
| 134 |
+
# deleting channels, should ping @alerts for this
|
| 135 |
+
embed = Embed(description=f'Channel {channel.name} ({channel.mention}) was deleted', color=Color.red())
|
| 136 |
+
await bot.log_channel.send(embed=embed)
|
| 137 |
+
|
| 138 |
+
@bot.event
|
| 139 |
+
async def on_guild_role_create(role):
|
| 140 |
+
# creating roles
|
| 141 |
+
embed = Embed(description=f'Role {role.mention} was created', color=Color.green())
|
| 142 |
+
await bot.log_channel.send(embed=embed)
|
| 143 |
+
|
| 144 |
+
@bot.event
|
| 145 |
+
async def on_guild_role_delete(role):
|
| 146 |
+
# deleting roles, should ping @alerts for this
|
| 147 |
+
embed = Embed(description=f'Role {role.name} ({role.mention}) was deleted', color=Color.red())
|
| 148 |
+
await bot.log_channel.send(embed=embed)
|
| 149 |
+
|
| 150 |
+
@bot.event
|
| 151 |
+
async def on_guild_role_update(before, after):
|
| 152 |
+
# editing roles, could expand this
|
| 153 |
+
if before.name != after.name:
|
| 154 |
+
embed = Embed(description=f'Role {before.mention} was renamed to {after.name}', color=Color.orange())
|
| 155 |
+
await bot.log_channel.send(embed=embed)
|
| 156 |
+
|
| 157 |
+
if before.permissions.administrator != after.permissions.administrator:
|
| 158 |
+
# changes involving the administrator permission / sensitive permissions (can help to prevent mistakes)
|
| 159 |
+
certain_role_id = 1106995261487710411 # role to ping (@alerts)
|
| 160 |
+
certain_role = after.guild.get_role(certain_role_id)
|
| 161 |
+
embed = Embed(description=f'Role {after.mention} had its administrator permission {"enabled" if after.permissions.administrator else "disabled"}', color=Color.red())
|
| 162 |
+
await bot.log_channel.send(content=certain_role.mention, embed=embed)
|
| 163 |
+
|
| 164 |
+
@bot.event
|
| 165 |
+
async def on_voice_state_update(member, before, after):
|
| 166 |
+
if before.mute != after.mute:
|
| 167 |
+
# muting members
|
| 168 |
+
embed = Embed(description=f'{member} was {"muted" if after.mute else "unmuted"} in voice chat', color=Color.orange())
|
| 169 |
+
await bot.log_channel.send(embed=embed)
|
| 170 |
+
|
| 171 |
+
if before.deaf != after.deaf:
|
| 172 |
+
# deafening members
|
| 173 |
+
embed = Embed(description=f'{member} was {"deafened" if after.deaf else "undeafened"} in voice chat', color=Color.orange())
|
| 174 |
+
await bot.log_channel.send(embed=embed)
|
| 175 |
+
|
| 176 |
+
# bot stuff ---------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| 177 |
+
|
| 178 |
+
# send messages
|
| 179 |
+
#
|
| 180 |
+
|
| 181 |
+
@bot.event
|
| 182 |
+
async def on_ready():
|
| 183 |
+
print('Logged on as', bot.user)
|
| 184 |
+
bot.log_channel = bot.get_channel(1100458786826747945)
|
| 185 |
|
| 186 |
|
| 187 |
+
|
|
|
|
| 188 |
def run_bot():
|
| 189 |
bot.run(DISCORD_TOKEN)
|
| 190 |
+
|
| 191 |
threading.Thread(target=run_bot).start()
|
| 192 |
+
|
| 193 |
def greet(name):
|
| 194 |
return "Hello " + name + "!"
|
| 195 |
+
|
| 196 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 197 |
demo.launch()
|