This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
166 lines (138 loc) · 6.17 KB
/
main.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
# coding: utf-8
import json
import os
import re
import traceback
from os.path import dirname, join
import discord
import requests
import sentry_sdk
from discord.ext import commands
from dotenv import load_dotenv
from cogs.commands.manage_help import BotHelp
from cogs.utils.config import Config
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
if os.name == "nt":
import tokens
token1 = tokens.token2
local = True
elif os.name == "posix":
token1 = os.environ["token1"]
local = False
sentry_sdk.init(os.environ["sentry_url"], traces_sample_rate=1.0)
def _prefix_callable(bot, msg: discord.Message):
base = [f"<@{bot.user.id}> ", f"<@!{bot.user.id}> "]
if msg.guild is None:
base.append('/')
else:
base.append(bot.prefixes.get(msg.guild.id, '/'))
return base
class MyBot(commands.Bot):
def __init__(self, **options):
allowed_mentions = discord.AllowedMentions(everyone=False, roles=False, users=True)
intents = discord.Intents.all()
super().__init__(command_prefix=_prefix_callable, allowed_mentions=allowed_mentions,
intents=intents, help_command=BotHelp(), **options)
self.local = local
self.guild_invite_url = "https://discord.gg/bQWsu3Z"
self.invite_url = "https://discord.com/oauth2/authorize?client_id=627143285906866187&permissions=805660736&scope=bot"
# guild_id: prefix
self.prefixes = Config('prefixes.json')
# user_id or guild_id to True
self.blacklist = Config('blacklist.json')
# setting: guild_id: True
self.settings = Config('settings.json')
if not local:
path = f"/home/{os.environ['user']}/2rz-bot"
else:
path = "."
self.load_extension('jishaku')
dirs = ["commands", "events", "guilds"]
for dire in dirs:
for cog in os.listdir(f"{path}/cogs/{dire}"):
if cog.endswith('.py'):
try:
self.load_extension(f'cogs.{dire}.{cog[:-3]}')
except Exception:
traceback.print_exc()
@self.check
async def checks(ctx):
if self.blacklist.is_key(ctx.author.id):
return False
if not ctx.channel.permissions_for(ctx.me).send_messages:
return False
return True
async def on_ready(self): # botが起動したら
print(self.user.name)
print(self.user.id)
print(discord.__version__)
print("--------")
@staticmethod
def get_mined_block(uuid: str) -> int:
"""整地鯖のapiからこれまでに掘ったブロック数を取得"""
resp = requests.get(f'https://ranking-gigantic.seichi.click/api/ranking/player/{uuid}?types=break')
data_json = json.loads(resp.text)
data = data_json[0]["data"]["raw_data"]
return int(data)
def get_shared_count(self, user: discord.User) -> int:
"""ユーザとbotの共通のサーバーの数を取得"""
return sum(g.get_member(user.id) is not None for g in self.guilds)
async def quote(self, message: discord.Message) -> None:
for url in re.findall(r"https://(?:ptb.|canary.)?discord(?:app)?.com/channels/[0-9]+/[0-9]+/[0-9]+", message.content):
try:
channel_id, message_id = map(int, url.split("/")[-2:])
ch = self.get_channel(channel_id)
if ch is None:
continue
msg = await ch.fetch_message(message_id)
def quote_reaction(msg, embed):
if msg.reactions:
reaction_send = ''
for reaction in msg.reactions:
emoji = reaction.emoji
count = str(reaction.count)
reaction_send = f'{reaction_send}{emoji}{count} '
embed.add_field(
name='reaction', value=reaction_send, inline=False)
return embed
if msg.embeds or msg.content or msg.attachments:
embed = discord.Embed(
description=msg.content,
timestamp=msg.created_at)
embed.set_author(
name=msg.author, icon_url=msg.author.avatar_url)
embed.set_footer(
text=msg.channel.name,
icon_url=msg.guild.icon_url)
if msg.attachments:
embed.set_image(url=msg.attachments[0].url)
embed = quote_reaction(msg, embed)
if msg.content or msg.attachments:
await message.channel.send(embed=embed)
if len(msg.attachments) >= 2:
for attachment in msg.attachments[1:]:
embed = discord.Embed().set_image(url=attachment.url)
await message.channel.send(embed=embed)
for embed in msg.embeds:
embed = quote_reaction(msg, embed)
await message.channel.send(embed=embed)
elif msg.system_content:
embed = discord.Embed(
description=f"{msg.system_content}\n\n:warning:これはシステムメッセージです。",
timestamp=msg.created_at)
embed.set_author(
name=msg.author, icon_url=msg.author.avatar_url)
embed.set_footer(
text=msg.channel.name,
icon_url=msg.guild.icon_url)
embed = quote_reaction(msg, embed)
await message.channel.send(embed=embed)
else:
await message.channel.send('メッセージIDは存在しますが、内容がありません')
except discord.errors.NotFound:
await message.channel.send("指定したメッセージが見つかりません")
if __name__ == "__main__":
bot = MyBot()
bot.run(token1)