Skip to content

Instantly share code, notes, and snippets.

@meto4d
Last active August 22, 2019 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meto4d/d019ca008b5c23e677ab0f6fc6107b1b to your computer and use it in GitHub Desktop.
Save meto4d/d019ca008b5c23e677ab0f6fc6107b1b to your computer and use it in GitHub Desktop.
https://github.com/meto4d/NanVDiscordbot/ のDiscordbotの最小限の機能を適当に残したもの
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import discord # インスコした discord.py
import re # 正規表現
import asyncio
##### NEED FILL IN THIS BLANK #####
auth_token = ""
print('ログイン中...')
client = discord.Client() #接続に使用するオブジェクト
# 起動時に通知してくれる処理
@client.event
async def on_ready():
print(client.user.name + " " + str(client.user.id))
print('ログインしました')
# メッセージを受信したときの処理
@client.event
async def on_message(message):
# steamURL が発言されたら steam:// でURLを返す処理
await DiceRoll(message, client)
await CountDown(message, client)
await testLogs(message, client)
# メッセージを消すテスト
delPattern = r"/neko"
delMatch = re.match(delPattern, message.content)
if delMatch:
await client.delete_message(message)
# 行頭でメンションが来たときの処理
if message.content.startswith("<@"+client.user.id+">"):
if (await CountDown(message, client)):
pass
else:
reply = f'{message.author.mention} コマンドが認識できませんでした'
await client.send_message(message.channel, reply)
###################
#
###################
# サイコロ
async def DiceRoll(msg, cl):
pattern = r"^(\d+)D(\d+)"
matchOB = re.match(pattern, msg.content, re.IGNORECASE)
if matchOB:
num = int(matchOB.group(1))
if num > 100:
await cl.send_message(msg.channel, "101以上は実行できません")
return
cube = int(matchOB.group(2))
randsum = 0
randlist = '{'
for i in range(num):
tmp = random.randint(1, cube)
randsum += tmp
randlist += str(tmp)
randlist += ','
if (i != 0) and (i % 50 == 0):
randlist += '\n'
randlist = randlist[:len(randlist) - 1]
randlist += '}'
em = discord.Embed(description=randlist)
await cl.send_message(msg.channel, str(randsum), embed=em)
# カウントダウン
async def CountDown(msg, cl):
pattern = r"カウントダウン"
matchOB = re.search(pattern, msg.content, re.IGNORECASE)
if matchOB:
for i in range(10):
await cl.send_message(msg.author, str(10 - i))
await asyncio.sleep(10)
return True
return False
# logs_from test
async def testLogs(msg, cl):
if msg.content.startswith('!test'):
counter = 0
tmp = await cl.send_message(msg.channel, 'Calculating messages...')
async for log in cl.logs_from(msg.channel, limit=100):
if log.author == msg.author:
counter += 1
await cl.edit_message(tmp, 'You have {} messages.'.format(counter))
# botの接続と起動
#
client.run(auth_token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment