Spaces:
Running
Running
import discord | |
import json | |
import os | |
import requests | |
import time | |
import openai | |
import random | |
intents = discord.Intents.default() | |
intents.message_content = True | |
client = discord.Client(intents=intents) | |
config = json.loads(os.getenv("CONFIG")) | |
keys: list[str] = config["api_key"] | |
async def on_ready(): | |
print('We have logged in as {0.user}'.format(client)) | |
async def on_message(message): | |
print(f'{message.author}: {message.content}') | |
if message.author == client.user: | |
return | |
elif message.content == '.hello': | |
await message.channel.send('Hello!') | |
elif message.content == '.time': | |
await message.channel.send(time.asctime(time.localtime(time.time())) + ' UTC') | |
elif message.content == '.quote': | |
api = "https://v1.hitokoto.cn/" | |
response = requests.get(api) | |
json_data = json.loads(response.text) | |
await message.channel.send(json_data['hitokoto'] + ' ββ' + json_data['from']) | |
elif message.content == '.florrmap': | |
# map_url_old = 'https://raw.githubusercontents.com/xcx0902/images/main/florr/map-5.3.png' | |
map_url = 'https://raw.githubusercontents.com/xcx0902/images/main/florr/map24-4.13.png' | |
await message.channel.send(map_url) | |
elif message.content == '.florrserver': | |
await message.channel.send('Try to get server info from api...') | |
answer = '' | |
mpname = { | |
0: 'Garden ', | |
1: 'Desert ', | |
2: 'Ocean ', | |
3: 'Jungle ', | |
4: 'Ant Hell', | |
5: 'Sewers ' | |
} | |
for i in range(0, 6): | |
answer += mpname[i] + ' ' | |
api = 'https://api.n.m28.io/endpoint/florrio-map-' + str(i) + '-green/findEach/' | |
response = requests.get(api) | |
json_data = json.loads(response.text) | |
server_list = [('miami', 'US'), ('frankfurt', 'EU'), ('tokyo', 'AS')] | |
for (id, region) in server_list: | |
answer += '| ' + region + ': ' + json_data['servers']['vultr-' + id]['id'] + ' ' | |
answer += '\n' | |
await message.channel.send('```' + answer + '```') | |
await message.channel.send( | |
'Try using the following code in the browser console to change the server:\n' | |
'```javascript\ncp6.forceServerID(\'<serverid>\');\n```' | |
) | |
elif message.content.startswith('.tetriouser'): | |
username = message.content.split(' ')[1].lower() | |
api = 'https://ch.tetr.io/api/users/' + username | |
response = requests.get(api, headers={"User-Agent": "discord-bot"}) | |
json_data = json.loads(response.text) | |
if json_data['success'] is False: | |
await message.channel.send('User not found') | |
else: | |
await message.channel.send( | |
'The following data may be cached, so it may not be accurate:' | |
) | |
json_data = json_data['data'] | |
answer = '' | |
answer += f'Username: {json_data["username"].upper()}\n' | |
answer += f'ID: {json_data["_id"]}\n' | |
answer += f'XP: {json_data["xp"]}\n' | |
try: | |
json_data = json_data['league'] | |
rank = '?' if json_data['rank'] == 'z' else json_data['rank'] | |
answer += f'League Rank: {rank.upper()}\n' | |
answer += f'League Rating: {json_data["rating"]}\n' | |
answer += f'League GW/GP: {json_data["gameswon"]}/{json_data["gamesplayed"]}' | |
answer += f' ({json_data["gameswon"]/json_data["gamesplayed"]*100}%)\n' | |
best_rank = '?' if json_data['bestrank'] == 'z' else json_data['bestrank'] | |
answer += f'League Best Rank: {best_rank.upper()}\n' | |
answer += f'League APM: {json_data["apm"]}\n' | |
answer += f'League PPS: {json_data["pps"]}\n' | |
answer += f'League VS: {json_data["vs"]}\n' | |
answer += f'League Percentile: {json_data["percentile"]}\n' | |
except BaseException: | |
pass | |
await message.channel.send('```' + answer + '```') | |
elif message.content == '.help': | |
await message.channel.send( | |
'.hello : Say hello to the bot (You will get a hello back)\n' | |
'.time : Show the date and time now\n' | |
'.quote : Get a random quote from hitokoto\n' | |
'.florrmap : Show the florr.io map\n' | |
'.florrserver : Get florr.io server info\n' | |
'.tetriouser <username> : Get tetr.io user info\n' | |
'.help : Show this message' | |
) | |
else: | |
try: | |
if len(keys) == 0: | |
raise openai.RateLimitError("All API keys have been used up.") | |
key = random.choice(keys) | |
response = openai.OpenAI( | |
api_key=key, | |
base_url=config["url"], | |
default_headers={ | |
"User-Agent": config.get("user_agent", ""), | |
"Cookie": config.get("cookie", "") | |
} | |
).chat.completions.create( | |
model=config["model"], | |
messages=[ | |
{"role": "user", "content": message.content} | |
], | |
stream=False | |
).choices[0].message.content | |
await message.channel.send(response) | |
except openai.RateLimitError as e: | |
if str(e).find("You've reached the upper limit for today's usage.") != -1: | |
print(f"[Rate Limited] Remove key {key[:8]}...{key[-8:]}") | |
keys.remove(key) | |
raise | |
# print(os.getenv("TOKEN")) | |
client.run(os.getenv('TOKEN')) |