Spaces:
Running
Running
File size: 5,665 Bytes
bb6a34f 88d4782 bb6a34f 88d4782 bb6a34f 88d4782 bb6a34f 88d4782 bb6a34f 88d4782 bb6a34f 88d4782 bb6a34f 88d4782 bb6a34f 88d4782 bb6a34f 675140f bb6a34f 864baee bb6a34f 88d4782 bb6a34f 88d4782 2be4d18 bb6a34f 46bd13a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
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"]
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
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')) |